Browse files

Only add models with an id to the store. Closes #411

  • Loading branch information...
1 parent 1c8c232 commit 2dc59ca5ff3283b7c50091e3bc8d86a9befd2813 @PaulUithol committed Jan 22, 2014
Showing with 42 additions and 9 deletions.
  1. +20 −3 backbone-relational.js
  2. +22 −6 test/tests.js
View
23 backbone-relational.js
@@ -459,7 +459,6 @@
if ( coll ) {
var modelColl = model.collection;
coll.add( model );
- this.listenTo( model, 'destroy', this.unregister, this );
this.listenTo( model, 'relational:unregister', this.unregister, this );
model.collection = modelColl;
}
@@ -489,6 +488,11 @@
*/
update: function( model ) {
var coll = this.getCollection( model );
+
+ if ( !coll.contains( model ) ) {
+ this.register( model );
+ }
+
// This triggers updating the lookup indices kept in a collection
coll._onModelEvent( 'change:' + model.idAttribute, model, coll );
@@ -510,7 +514,12 @@
});
var coll = this.getCollection( model );
- coll && coll.remove( model, options );
+ if ( coll.contains( model ) ) {
+ coll.remove( model, options );
+ }
+ else {
+ coll.trigger( 'relational:remove', model, coll );
+ }
},
/**
@@ -1232,6 +1241,10 @@
changed && Backbone.Model.prototype.trigger.apply( dit, args );
});
}
+ else if ( eventName === 'destroy' ) {
+ Backbone.Model.prototype.trigger.apply( this, arguments );
+ Backbone.Relational.store.unregister( this );
+ }
else {
Backbone.Model.prototype.trigger.apply( this, arguments );
}
@@ -1466,7 +1479,11 @@
// Ideal place to set up relations, if this is the first time we're here for this model
if ( !this._isInitialized && !this.isLocked() ) {
this.constructor.initializeModelHierarchy();
- Backbone.Relational.store.register( this );
+
+ if ( newId || newId === 0 ) {
+ Backbone.Relational.store.register( this );
+ }
+
this.initializeRelations( options );
}
// The store should know about an `id` update asap
View
28 test/tests.js
@@ -4472,12 +4472,26 @@ $(document).ready(function() {
test( "Creation and destruction", 0, function() {
+ var registerCount = 0,
+ unregisterCount = 0,
+ register = Backbone.Store.prototype.register,
+ unregister = Backbone.Store.prototype.unregister;
+
+ Backbone.Store.prototype.register = function( model ) {
+ registerCount++;
+ return register.apply( this, arguments );
+ };
+ Backbone.Store.prototype.unregister = function( model, coll, options ) {
+ unregisterCount++;
+ return unregister.apply( this, arguments );
+ };
+
var addHasManyCount = 0,
addHasOneCount = 0,
tryAddRelatedHasMany = Backbone.HasMany.prototype.tryAddRelated,
tryAddRelatedHasOne = Backbone.HasOne.prototype.tryAddRelated;
- Backbone.HasMany.prototype.tryAddRelated = function( model, coll, options ) {
+ Backbone.Store.prototype.tryAddRelated = function( model, coll, options ) {
addHasManyCount++;
return tryAddRelatedHasMany.apply( this, arguments );
};
@@ -4517,17 +4531,17 @@ $(document).ready(function() {
reverseRelation: {
key: 'parent'
}
- }]
- });
-
- var Parents = Backbone.Collection.extend({
- model: Parent,
+ }],
toString: function() {
return this.get( 'name' );
}
});
+ var Parents = Backbone.Collection.extend({
+ model: Parent
+ });
+
// bootstrap data
@@ -4634,6 +4648,8 @@ $(document).ready(function() {
var secs = (new Date() - start) / 1000;
console.log( 'data loaded in %s, removeHasManyCount=%o, removeHasOneCount=%o', secs, removeHasManyCount, removeHasOneCount );
+
+ console.log( 'registerCount=%o, unregisterCount=%o', registerCount, unregisterCount );
});
});

0 comments on commit 2dc59ca

Please sign in to comment.