835f5406 by Michael Richards

Merge pull request #107 from obmarg/fix-each-binding

each-* will now insert between siblings [Closes #106]
2 parents b23347ae 910e7087
...@@ -145,6 +145,23 @@ describe('Functional', function() { ...@@ -145,6 +145,23 @@ describe('Functional', function() {
145 expect(el.getElementsByTagName('li')[0]).toHaveTheTextContent('a'); 145 expect(el.getElementsByTagName('li')[0]).toHaveTheTextContent('a');
146 expect(el.getElementsByTagName('li')[0].className).toBe('bar'); 146 expect(el.getElementsByTagName('li')[0].className).toBe('bar');
147 }); 147 });
148
149 it('should insert items between any surrounding elements', function(){
150 firstItem = document.createElement('li');
151 lastItem = document.createElement('li');
152 firstItem.textContent = 'first';
153 lastItem.textContent = 'last';
154 list.appendChild(lastItem);
155 list.insertBefore(firstItem, listItem);
156
157 listItem.setAttribute('data-text', 'item:name');
158
159 rivets.bind(el, bindData);
160 expect(el.getElementsByTagName('li')[0]).toHaveTheTextContent('first');
161 expect(el.getElementsByTagName('li')[1]).toHaveTheTextContent('a');
162 expect(el.getElementsByTagName('li')[2]).toHaveTheTextContent('b');
163 expect(el.getElementsByTagName('li')[3]).toHaveTheTextContent('last');
164 })
148 }); 165 });
149 }); 166 });
150 167
......
...@@ -344,7 +344,10 @@ Rivets.binders = ...@@ -344,7 +344,10 @@ Rivets.binders =
344 data[n] = m for n, m of @view.models 344 data[n] = m for n, m of @view.models
345 data[@args[0]] = item 345 data[@args[0]] = item
346 itemEl = el.cloneNode true 346 itemEl = el.cloneNode true
347 previous = @iterated[@iterated.length - 1] or @marker 347 if @iterated.length > 0
348 previous = @iterated[@iterated.length - 1].els[0]
349 else
350 previous = @marker
348 @marker.parentNode.insertBefore itemEl, previous.nextSibling ? null 351 @marker.parentNode.insertBefore itemEl, previous.nextSibling ? null
349 @iterated.push rivets.bind itemEl, data 352 @iterated.push rivets.bind itemEl, data
350 353
......