Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use itemSelector in internalCount initialization #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Resources/public/js/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
* objects behavior.
*/
window.infinite.Collection = function (collection, prototypes, options) {
this.$collection = $(collection);
this.internalCount = this.$collection.children().length;
this.$prototypes = prototypes;

this.options = $.extend({
allowAdd: true,
allowDelete: true,
Expand All @@ -52,6 +48,15 @@
keepScripts: false
}, options || {});

this.$collection = $(collection);
var nestedSelector = this.options.itemSelector;
for (var i = 0; i < this.$collection.parents(this.options.itemSelector).length; i++) {
nestedSelector += ' ' + this.options.itemSelector;
}

this.internalCount = this.$collection.find(this.options.itemSelector).not(nestedSelector + ' ' + this.options.itemSelector).length;
this.$prototypes = prototypes;

this.initialise();
};

Expand Down
26 changes: 23 additions & 3 deletions Tests/Javascript/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
equal(collection.internalCount, 4,
'Internal count is incremented when adding');
});

test("Keep scripts in prototype html", function() {
var collection = setUpCollection('#markup .list-collection-with-prototype-scripts', {
keepScripts: true
Expand Down Expand Up @@ -182,13 +182,33 @@
equal(result.length, 1, 'addToCollection returned the row');
});

function setUpCollection(selector, options) {
test("Custom html structure internalCount", function() {
var collection = setUpCollection('#markup .list-collection-different-html-structure');
equal(collection.internalCount, 1,
'Internal count is correctly initialized');
});

test("Nested collection internalCount", function() {
var collection = setUpCollection('#markup .list-collection-with-nested-collection');
equal(collection.internalCount, 1,
'Internal count with nested collections is correctly initialized');

var nestedcollection = setUpCollection('#markup .list-collection-with-nested-collection', { itemSelector: '.child-item' }, '.child-collection');
equal(nestedcollection.internalCount, 3,
'Internal count of nested collection is correctly initialized');

var nestedcollection = setUpCollection('#markup .list-collection-with-nested-collection', {}, '.third-level-collection');
equal(nestedcollection.internalCount, 2,
'Internal count of multiple nested collection is correctly initialized');
});

function setUpCollection(selector, options, elSelector) {
var $fixture = $('#qunit-fixture');

var $dom = $(selector).clone();
$dom.appendTo($fixture);

var colEl = $dom.find('.collection'),
var colEl = $dom.find(elSelector ? elSelector : '.collection'),
prototypes = $dom.find('.add_item');

collection = new window.infinite.Collection(colEl, prototypes, options);
Expand Down
51 changes: 51 additions & 0 deletions Tests/Javascript/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,57 @@
&lt;a class=&quot;custom_remove_item&quot;&gt;Remove&lt;/a&gt;
&lt;/div&gt;">Add</a>
</div>
<div class="list-collection-different-html-structure">
<table class="collection">
<thead>
<tr>
<th>Email</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<tr class="item input-append" data-original>
<td><input type="email" name="formb[0]" id="formb_0" /></td>
<td><a class="custom_remove_item">Remove</a></td>
</tr>
</tbody>
</table>

<a class="add_item" data-customprototype="&lt;tr class=&quot;customitem input-append&quot;&gt;
&lt;td&gt;&lt;input type=&quot;email&quot; name=&quot;formb[__customname__]&quot; id=&quot;formb___customname__&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;a class=&quot;custom_remove_item&quot;&gt;Remove&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;">Add</a>
</div>
<div class="list-collection-with-nested-collection">
<div class="collection">
<div class="item input-append" data-original>
<div class="child-collection">
<div class="child-item input-append" data-original>
<input type="email" name="forma[0]" id="forma_0" />
<a class="remove_item">Remove</a>
</div>
<div class="child-item input-append" data-original>
<input type="email" name="forma[1]" id="forma_1" />
<a class="remove_item">Remove</a>
</div>
<div class="child-item input-append" data-original>
<input type="email" name="forma[2]" id="forma_2" />
<a class="remove_item">Remove</a>
<div class="third-level-collection">
<div class="item input-append" data-original>
<input type="email" name="forma[2][0]" id="forma_2_0" />
<a class="remove_item">Remove</a>
</div>
<div class="item input-append" data-original>
<input type="email" name="forma[2][1]" id="forma_2_1" />
<a class="remove_item">Remove</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

</body>
Expand Down