Skip to content

Commit

Permalink
Check (and error on) duplicate ids when explicitly setting the idAttr…
Browse files Browse the repository at this point in the history
…ibute.

Closes #295
  • Loading branch information
PaulUithol committed Mar 29, 2013
1 parent e03d00b commit 51c6f89
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
46 changes: 30 additions & 16 deletions backbone-relational.js
Expand Up @@ -375,7 +375,7 @@
},

/**
*
* Find a specific model of a certain `type` in the store
* @param type
* @param {String|Number|Object|Backbone.RelationalModel} item
*/
Expand Down Expand Up @@ -404,20 +404,31 @@
var coll = this.getCollection( model );

if ( coll ) {
if ( coll.get( model ) ) {
if ( Backbone.Relational.showWarnings && typeof console !== 'undefined' ) {
console.warn( 'Duplicate id! Old RelationalModel=%o, new RelationalModel=%o', coll.get( model ), model );
}
throw new Error( "Cannot instantiate more than one Backbone.RelationalModel with the same id per type!" );
}

var modelColl = model.collection;
coll.add( model );
this.listenTo( model, 'destroy', this.unregister, this );
model.collection = modelColl;
}
},

/**
* Check if the given model may use the given `id`
* @param model
* @param [id]
*/
checkId: function( model, id ) {
var coll = this.getCollection( model ),
duplicate = coll && coll.get( id );

if ( duplicate && model !== duplicate ) {
if ( Backbone.Relational.showWarnings && typeof console !== 'undefined' ) {
console.warn( 'Duplicate id! Old RelationalModel=%o, new RelationalModel=%o', duplicate, model );
}

throw new Error( "Cannot instantiate more than one Backbone.RelationalModel with the same id per type!" );
}
},

/**
* Explicitly update a model's id in its store collection
* @param {Backbone.RelationalModel} model
Expand Down Expand Up @@ -1346,20 +1357,23 @@
attributes[ key ] = value;
}

var prevId = this.id,
result = Backbone.Model.prototype.set.apply( this, arguments );

// Ideal place to set up relations :)
try {
var id = this.id,
newId = attributes && this.idAttribute in attributes && attributes[ this.idAttribute ];

// Check if we're not setting a duplicate id before actually calling `set`.
Backbone.Relational.store.checkId( this, newId );

var result = Backbone.Model.prototype.set.apply( this, arguments );

// 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 );

this.initializeRelations( options );
}
// Update the 'idAttribute'; we don't want the store to miss an 'id' update (due to {silent:true})
else if ( attributes && this.idAttribute in attributes && prevId !== attributes[ this.idAttribute ] ) {
// The store should know about an `id` update asap
else if ( newId && newId !== id ) {
Backbone.Relational.store.update( this );
}

Expand Down
17 changes: 17 additions & 0 deletions test/tests.js
Expand Up @@ -663,6 +663,23 @@ $(document).ready(function() {

ok( Backbone.Relational.eventQueue.isBlocked() === false );
});

test( "Don't allow setting a duplicate `id`", 4, function() {
var a = new Zoo(); // This object starts with no id.
var b = new Zoo( { 'id': 42 } ); // This object starts with an id of 42.

equal( b.id, 42 );

try {
a.set( 'id', 42 );
}
catch( error ) {
ok( true, "Duplicate id error thrown" );
}

ok( !a.id, "a.id=" + a.id );
equal( b.id, 42 );
});

test( "Models are created from objects, can then be found, destroyed, cannot be found anymore", function() {
var houseId = 'house-10';
Expand Down

0 comments on commit 51c6f89

Please sign in to comment.