14b3c772 by Michael Richards

Merge pull request #111 from terrancesnyder/Issue#110

Pull request for #110
2 parents 835f5406 36de3dba
...@@ -116,6 +116,12 @@ describe('Functional', function() { ...@@ -116,6 +116,12 @@ describe('Functional', function() {
116 expect(el.getElementsByTagName('li').length).toBe(2); 116 expect(el.getElementsByTagName('li').length).toBe(2);
117 }); 117 });
118 118
119 it('should not fail if the collection being bound to is null', function() {
120 data.set({ items: null});
121 rivets.bind(el, bindData);
122 expect(el.getElementsByTagName('li').length).toBe(0);
123 });
124
119 it('should re-loop over the collection and create new instances when the array changes', function() { 125 it('should re-loop over the collection and create new instances when the array changes', function() {
120 rivets.bind(el, bindData); 126 rivets.bind(el, bindData);
121 expect(el.getElementsByTagName('li').length).toBe(2); 127 expect(el.getElementsByTagName('li').length).toBe(2);
...@@ -162,6 +168,7 @@ describe('Functional', function() { ...@@ -162,6 +168,7 @@ describe('Functional', function() {
162 expect(el.getElementsByTagName('li')[2]).toHaveTheTextContent('b'); 168 expect(el.getElementsByTagName('li')[2]).toHaveTheTextContent('b');
163 expect(el.getElementsByTagName('li')[3]).toHaveTheTextContent('last'); 169 expect(el.getElementsByTagName('li')[3]).toHaveTheTextContent('last');
164 }) 170 })
171
165 }); 172 });
166 }); 173 });
167 174
......
...@@ -339,17 +339,18 @@ Rivets.binders = ...@@ -339,17 +339,18 @@ Rivets.binders =
339 339
340 @iterated = [] 340 @iterated = []
341 341
342 for item in collection 342 if collection
343 data = {} 343 for item in collection
344 data[n] = m for n, m of @view.models 344 data = {}
345 data[@args[0]] = item 345 data[n] = m for n, m of @view.models
346 itemEl = el.cloneNode true 346 data[@args[0]] = item
347 if @iterated.length > 0 347 itemEl = el.cloneNode true
348 previous = @iterated[@iterated.length - 1].els[0] 348 if @iterated.length > 0
349 else 349 previous = @iterated[@iterated.length - 1].els[0]
350 previous = @marker 350 else
351 @marker.parentNode.insertBefore itemEl, previous.nextSibling ? null 351 previous = @marker
352 @iterated.push rivets.bind itemEl, data 352 @marker.parentNode.insertBefore itemEl, previous.nextSibling ? null
353 @iterated.push rivets.bind itemEl, data
353 354
354 "class-*": (el, value) -> 355 "class-*": (el, value) ->
355 elClass = " #{el.className} " 356 elClass = " #{el.className} "
......