Skip to content

Commit 3d68787

Browse files
net02Daniele Bartocci
authored andcommitted
Use itemSelector in internalCount initialization
Fixes a bug with an incorrect internalCount when the html does not have collection items as direct children of the collection, i.e: <table data-form-widget="collection"> <thead>...</thead> <tbody class="items"> <tr class="item">...</tr> <tr class="item">...</tr> </tbody> </table>
1 parent 12f8953 commit 3d68787

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

Resources/public/js/collections.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
* objects behavior.
3838
*/
3939
window.infinite.Collection = function (collection, prototypes, options) {
40-
this.$collection = $(collection);
41-
this.internalCount = this.$collection.children().length;
42-
this.$prototypes = prototypes;
43-
4440
this.options = $.extend({
4541
allowAdd: true,
4642
allowDelete: true,
@@ -52,6 +48,15 @@
5248
keepScripts: false
5349
}, options || {});
5450

51+
this.$collection = $(collection);
52+
var nestedSelector = this.options.itemSelector;
53+
for (var i = 0; i < this.$collection.parents(this.options.itemSelector).length; i++) {
54+
nestedSelector += ' ' + this.options.itemSelector;
55+
}
56+
57+
this.internalCount = this.$collection.find(this.options.itemSelector).not(nestedSelector + ' ' + this.options.itemSelector).length;
58+
this.$prototypes = prototypes;
59+
5560
this.initialise();
5661
};
5762

Tests/Javascript/collection_tests.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
equal(collection.internalCount, 4,
133133
'Internal count is incremented when adding');
134134
});
135-
135+
136136
test("Keep scripts in prototype html", function() {
137137
var collection = setUpCollection('#markup .list-collection-with-prototype-scripts', {
138138
keepScripts: true
@@ -182,13 +182,33 @@
182182
equal(result.length, 1, 'addToCollection returned the row');
183183
});
184184

185-
function setUpCollection(selector, options) {
185+
test("Custom html structure internalCount", function() {
186+
var collection = setUpCollection('#markup .list-collection-different-html-structure');
187+
equal(collection.internalCount, 1,
188+
'Internal count is correctly initialized');
189+
});
190+
191+
test("Nested collection internalCount", function() {
192+
var collection = setUpCollection('#markup .list-collection-with-nested-collection');
193+
equal(collection.internalCount, 1,
194+
'Internal count with nested collections is correctly initialized');
195+
196+
var nestedcollection = setUpCollection('#markup .list-collection-with-nested-collection', { itemSelector: '.child-item' }, '.child-collection');
197+
equal(nestedcollection.internalCount, 3,
198+
'Internal count of nested collection is correctly initialized');
199+
200+
var nestedcollection = setUpCollection('#markup .list-collection-with-nested-collection', {}, '.third-level-collection');
201+
equal(nestedcollection.internalCount, 2,
202+
'Internal count of multiple nested collection is correctly initialized');
203+
});
204+
205+
function setUpCollection(selector, options, elSelector) {
186206
var $fixture = $('#qunit-fixture');
187207

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

191-
var colEl = $dom.find('.collection'),
211+
var colEl = $dom.find(elSelector ? elSelector : '.collection'),
192212
prototypes = $dom.find('.add_item');
193213

194214
collection = new window.infinite.Collection(colEl, prototypes, options);

Tests/Javascript/test.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,57 @@
5555
&lt;a class=&quot;custom_remove_item&quot;&gt;Remove&lt;/a&gt;
5656
&lt;/div&gt;">Add</a>
5757
</div>
58+
<div class="list-collection-different-html-structure">
59+
<table class="collection">
60+
<thead>
61+
<tr>
62+
<th>Email</th>
63+
<th>Remove</th>
64+
</tr>
65+
</thead>
66+
<tbody>
67+
<tr class="item input-append" data-original>
68+
<td><input type="email" name="formb[0]" id="formb_0" /></td>
69+
<td><a class="custom_remove_item">Remove</a></td>
70+
</tr>
71+
</tbody>
72+
</table>
73+
74+
<a class="add_item" data-customprototype="&lt;tr class=&quot;customitem input-append&quot;&gt;
75+
&lt;td&gt;&lt;input type=&quot;email&quot; name=&quot;formb[__customname__]&quot; id=&quot;formb___customname__&quot; /&gt;&lt;/td&gt;
76+
&lt;td&gt;&lt;a class=&quot;custom_remove_item&quot;&gt;Remove&lt;/a&gt;&lt;/td&gt;
77+
&lt;/tr&gt;">Add</a>
78+
</div>
79+
<div class="list-collection-with-nested-collection">
80+
<div class="collection">
81+
<div class="item input-append" data-original>
82+
<div class="child-collection">
83+
<div class="child-item input-append" data-original>
84+
<input type="email" name="forma[0]" id="forma_0" />
85+
<a class="remove_item">Remove</a>
86+
</div>
87+
<div class="child-item input-append" data-original>
88+
<input type="email" name="forma[1]" id="forma_1" />
89+
<a class="remove_item">Remove</a>
90+
</div>
91+
<div class="child-item input-append" data-original>
92+
<input type="email" name="forma[2]" id="forma_2" />
93+
<a class="remove_item">Remove</a>
94+
<div class="third-level-collection">
95+
<div class="item input-append" data-original>
96+
<input type="email" name="forma[2][0]" id="forma_2_0" />
97+
<a class="remove_item">Remove</a>
98+
</div>
99+
<div class="item input-append" data-original>
100+
<input type="email" name="forma[2][1]" id="forma_2_1" />
101+
<a class="remove_item">Remove</a>
102+
</div>
103+
</div>
104+
</div>
105+
</div>
106+
</div>
107+
</div>
108+
</div>
58109
</div>
59110

60111
</body>

0 commit comments

Comments
 (0)