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() {
expect(el.getElementsByTagName('li')[0]).toHaveTheTextContent('a');
expect(el.getElementsByTagName('li')[0].className).toBe('bar');
});
it('should insert items between any surrounding elements', function(){
firstItem = document.createElement('li');
lastItem = document.createElement('li');
firstItem.textContent = 'first';
lastItem.textContent = 'last';
list.appendChild(lastItem);
list.insertBefore(firstItem, listItem);
listItem.setAttribute('data-text', 'item:name');
rivets.bind(el, bindData);
expect(el.getElementsByTagName('li')[0]).toHaveTheTextContent('first');
expect(el.getElementsByTagName('li')[1]).toHaveTheTextContent('a');
expect(el.getElementsByTagName('li')[2]).toHaveTheTextContent('b');
expect(el.getElementsByTagName('li')[3]).toHaveTheTextContent('last');
})
});
});
......
......@@ -344,7 +344,10 @@ Rivets.binders =
data[n] = m for n, m of @view.models
data[@args[0]] = item
itemEl = el.cloneNode true
previous = @iterated[@iterated.length - 1] or @marker
if @iterated.length > 0
previous = @iterated[@iterated.length - 1].els[0]
else
previous = @marker
@marker.parentNode.insertBefore itemEl, previous.nextSibling ? null
@iterated.push rivets.bind itemEl, data
......