Permalink
Browse files

Rebuild key to index mappings on delete.

  • Loading branch information...
1 parent 07f7269 commit 762362722e2282b617ccccfb53747eead57189b1 Michael Aufreiter committed May 2, 2012
Showing with 11 additions and 6 deletions.
  1. +11 −6 data.js
View
@@ -460,15 +460,20 @@
c.objects = _.clone(objects);
c.length = objects.length;
- // Register keys for fast lookup
- _.each(objects, function(o, i) {
- c.keys[o._id] = i;
- });
+ c.buildKeys();
return c;
};
_.extend(Data.Collection.prototype, _.Events, Data.Query, {
+ // Register keys for fast lookup
+ buildKeys: function() {
+ this.keys = {};
+ _.each(this.objects, function(o, i) {
+ this.keys[o._id] = i;
+ }, this);
+ },
+
// Get an object (item) from the collection
get: function(id) {
if (id.match('^/type/')) return this.type;
@@ -515,13 +520,13 @@
return o;
},
- // Delete object at given *key*
+ // Delete object at given key
del: function (key) {
if (this.keys.hasOwnProperty(key)) {
var l = this.length;
var index = this.keys[key];
- delete this.keys[key];
this.objects.splice(index, 1);
+ this.buildKeys(); // costly
this.length = l-1;
}
return this;

0 comments on commit 7623627

Please sign in to comment.