From db29763f96042f0becc6e804ff5991b0704efe01 Mon Sep 17 00:00:00 2001 From: Paul Uithol Date: Fri, 8 Jul 2011 20:35:23 +0200 Subject: [PATCH] Allow the use of numeric keys as well (fixes issue #12) --- backbone-relational.js | 18 +++++++++++------- test/tests.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/backbone-relational.js b/backbone-relational.js index 56bd5324..3d93c63b 100644 --- a/backbone-relational.js +++ b/backbone-relational.js @@ -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 ); } @@ -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 ); } @@ -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 ) ) { @@ -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 ); @@ -858,6 +858,10 @@ } }, + /** + * Get the created relations for this model + * @return {array} + */ getRelations: function() { return this._relations; }, @@ -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); diff --git a/test/tests.js b/test/tests.js index 3d45fe3b..cab549aa 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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 } );