Kenneth Jørgensen avatar Kenneth Jørgensen committed 9dec7e3

Fixed Collection to handle removals and additions during iterations.

Comments (0)

Files changed (4)

 * *Fix:* toJSON ignored all defined relations.
 * *Fix:* `HasManyRelation` will no longer lose existing `Model` instances when set with a list of IDs.
 * *Fix:* Old value was not supplied on `Model` change events.
+* *Fix:* `Collection` can now add and remove entries during iteration.
 
 ## 0.0.1 (2013-07-30)
 * Initial version after splitting modelling off from Antifreeze.
-/*! Discrete 0.1.0-dev.3 - MIT license */
+/*! Discrete 0.1.0-dev.4 - MIT license */
 (function() {
   var Async, Calamity, Collection, Discrete, HasManyRelation, HasOneRelation, Loader, Map, Model, ModelRepo, Persistor, Relation, RepoPersistor, Set, calamity, exports, object_toString, root, _, _ref, _ref1,
     __hasProp = {}.hasOwnProperty,
   }
 
   Discrete = {
-    version: "0.1.0-dev.3"
+    version: "0.1.0-dev.4"
   };
 
   if (typeof exports !== "undefined") {
 
     Collection.prototype.each = function(fn) {
       var entry, index, _i, _len, _ref;
-      _ref = this._items;
+      _ref = _.clone(this._items);
       for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
         entry = _ref[index];
         fn.apply(this, [entry, index]);

spec/CollectionSpec.coffee

 		collection.add val3
 		expect(clone.size()).toBe 2
 
+	it "should handle additions and removals during iteration", ->
+		collection.addAll val1, val2
+		n = 0
+		collection.each (val, index) ->
+			if index is 0
+				expect(val).toBe val1
+				collection.remove val2
+			if index is 1
+				expect(val).toBe val2
+				collection.add val2
+			n++
+		expect(n).toBe 2
+
 	describe "change events", ->
 		change = null
 		changeEvent = null

src/Collection.coffee

 
 	# Iterator.
 	each: (fn) ->
-		for entry, index in @_items
+		for entry, index in _.clone @_items
 			fn.apply @, [entry, index]
 		return @
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.