910e7087 by Graeme Coupar

each-* will now insert between siblings [Closes #106]

This commit fixes an issue with the each-* binding where all but the
first element would be inserted into incorrect positions when the
binding is used on an element with siblings.

The issue occurred because the code was passing a Rivets.View instance
in to insertBefore for all but the first element, which just caused the
element to be inserted last.
1 parent b23347ae
...@@ -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
......