Permalink
Browse files

Fixed arrayRemove() helper function

The function was broken in a way that when it actually removed an item from
the array, it would run over the end of the array, throwing an exception
trying to work with a null element. Fixed by returning directly after
deleting an element. This is OK since the method is only used for arrays
without duplicate elements.

Also added a test case that uses arrayRemove() (indirectly).
  • Loading branch information...
1 parent e549acf commit e0d776b10213cf18f69b16160800555a7f0242e0 @superquadratic superquadratic committed Mar 30, 2012
Showing with 20 additions and 0 deletions.
  1. +2 −0 lib/persistence.js
  2. +18 −0 test/browser/test.persistence.js
View
@@ -1286,8 +1286,10 @@ persistence.get = function(arg1, arg2) {
var el = ar[i];
if(el.equals && el.equals(item)) {
ar.splice(i, 1);
+ return;
} else if(el === item) {
ar.splice(i, 1);
+ return;
}
}
}
@@ -228,6 +228,24 @@ $(document).ready(function(){
});
});
+ asyncTest("Many-to-many with local changes", function() {
+ var t = new Task({name: "Some task"});
+ persistence.add(t);
+ t.tags.list(function(tags) {
+ equals(tags.length, 0, "Initially, no tags");
+ var tag1 = new Tag({name: "important"});
+ var tag2 = new Tag({name: "today"});
+ t.tags.add(tag1);
+ t.tags.add(tag2);
+ t.tags.remove(tag1);
+ t.tags.list(function(tags) {
+ equals(tags.length, 1, "2 tags added, 1 removed");
+ equals(tags[0].id, tag2.id, "Correct tag left");
+ start();
+ });
+ });
+ });
+
module("Query collections", {
setup: function() {
stop();

0 comments on commit e0d776b

Please sign in to comment.