Skip to content

Commit 85c6f8f

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 85c6f8f

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

Resources/public/js/collections.js

Lines changed: 4 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,10 @@
5248
keepScripts: false
5349
}, options || {});
5450

51+
this.$collection = $(collection);
52+
this.internalCount = this.$collection.find(this.options.itemSelector).length;
53+
this.$prototypes = prototypes;
54+
5555
this.initialise();
5656
};
5757

Tests/Javascript/collection_tests.js

Lines changed: 7 additions & 1 deletion
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,6 +182,12 @@
182182
equal(result.length, 1, 'addToCollection returned the row');
183183
});
184184

185+
test("Custom html structure", function() {
186+
var collection = setUpCollection('#markup .list-collection-different-html-structure');
187+
equal(collection.internalCount, 1,
188+
'Internal count is correctly initialized');
189+
});
190+
185191
function setUpCollection(selector, options) {
186192
var $fixture = $('#qunit-fixture');
187193

Tests/Javascript/test.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@
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>
5879
</div>
5980

6081
</body>

0 commit comments

Comments
 (0)