Skip to content

Commit

Permalink
Allow the use of numeric keys as well (fixes issue #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulUithol committed Jul 8, 2011
1 parent 66426bd commit db29763
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
18 changes: 11 additions & 7 deletions backbone-relational.js
Expand Up @@ -478,9 +478,9 @@
if ( item instanceof this.relatedModel ) {
model = item;
}
else if ( item && ( _.isString( item ) || typeof( item ) === 'object' ) ) {
else if ( item && ( _.isString( item ) || _.isNumber( item ) || typeof( item ) === 'object' ) ) {
// Try to find an instance of the appropriate 'relatedModel' in the store, or create it
var id = _.isString( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
var id = _.isString( item ) || _.isNumber( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
model = Backbone.Relational.store.find( this.relatedModel, id ) || this.createModel( item );
}

Expand Down Expand Up @@ -554,8 +554,8 @@
options = this.sanitizeOptions( options );

var item = this.keyContents;
if ( item && ( _.isString( item ) || typeof( item ) === 'object' ) ) {
var id = _.isString( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
if ( item && ( _.isString( item ) || _.isNumber( item ) || typeof( item ) === 'object' ) ) {
var id = _.isString( item ) || _.isNumber( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
if ( model.id === id ) {
this.addRelated( model, options );
}
Expand Down Expand Up @@ -623,7 +623,7 @@
if ( this.keyContents && _.isArray( this.keyContents ) ) {
// Try to find instances of the appropriate 'relatedModel' in the store
_.each( this.keyContents, function( item ) {
var id = _.isString( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
var id = _.isString( item ) || _.isNumber( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
var model = Backbone.Relational.store.find( this.relatedModel, id ) || this.createModel( item );

if ( model && !this.related.getByCid( model ) && !this.related.get( model ) ) {
Expand Down Expand Up @@ -674,7 +674,7 @@
if ( !this.related.getByCid( model ) && !this.related.get( model ) ) {
// Check if this new model was specified in 'this.keyContents'
var item = _.any( this.keyContents, function( item ) {
var id = _.isString( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
var id = _.isString( item ) || _.isNumber( item ) ? item : item[ this.relatedModel.prototype.idAttribute ];
return id && id === model.id;
}, this );

Expand Down Expand Up @@ -858,6 +858,10 @@
}
},

/**
* Get the created relations for this model
* @return {array}
*/
getRelations: function() {
return this._relations;
},
Expand Down Expand Up @@ -973,7 +977,7 @@
}

//console.debug( 'calling _add on coll=%o; model=%s (%o), options=%o', this, model.cid, model, options );
if ( !this.get( model ) && !this.getByCid( model ) ) {
if ( !( model instanceof Backbone.Model ) || !( this.get( model ) || this.getByCid( model ) ) ) {
model = _add.call( this, model, options );
}
this.trigger('relational:add', model, this, options);
Expand Down
38 changes: 38 additions & 0 deletions test/tests.js
Expand Up @@ -958,6 +958,44 @@ $(document).ready(function() {
ok( person1.get('likesALot') === person2 );
ok( person2.get('likedALotBy' ) === person1 );
});

test("Numerical keys", function() {
var child1 = new Node({ id: 2, name: 'First child' });
var parent = new Node({ id: 1, children: [2, 3], name: 'Parent' });
var child2 = new Node({ id: 3, name: 'Second child' });

equal( parent.get('children').length, 2 );
ok( parent.get('children').include( child1 ) );
ok( parent.get('children').include( child2 ) );

ok( child1.get('parent') === parent );
equal( child1.get('children').length, 0 );

ok( child2.get('parent') === parent );
equal( child2.get('children').length, 0 );
});

test("Relations that use refs to other models (instead of keys)", function() {
var child1 = new Node({ id: 2, name: 'First child' });
var parent = new Node({ id: 1, children: [child1, 3], name: 'Parent' });
var child2 = new Node({ id: 3, name: 'Second child' });

ok( child1.get('parent') === parent );
equal( child1.get('children').length, 0 );

equal( parent.get('children').length, 2 );
ok( parent.get('children').include( child1 ) );
ok( parent.get('children').include( child2 ) );

var child3 = new Node({ id: 4, parent: parent, name: 'Second child' });

equal( parent.get('children').length, 3 );
ok( parent.get('children').include( child3 ) );

ok( child3.get('parent') === parent );
equal( child3.get('children').length, 0 );
console.debug( parent );
});


module("Model loading", { setup: initObjects } );
Expand Down

0 comments on commit db29763

Please sign in to comment.