From 487f36526ef1b861eeab3fcecf93b1102ef48fd6 Mon Sep 17 00:00:00 2001 From: Phil Freo Date: Tue, 9 Jul 2013 17:17:24 -0700 Subject: [PATCH 1/4] Fix typo in tests --- test/tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests.js b/test/tests.js index 9bdc0022..a0ca61c8 100644 --- a/test/tests.js +++ b/test/tests.js @@ -1064,7 +1064,7 @@ $(document).ready(function() { var a1 = new Animal( { id: 'a1' } ); zooJSON = zoo.toJSON(); - equal( zooJSON.animals.length, 1, "0 animals in zooJSON; it serializes an array of attributes" ); + equal( zooJSON.animals.length, 1, "1 animals in zooJSON; it serializes an array of attributes" ); // Agent -> Customer; `idAttribute` on a HasMany var agent = new Agent({ id: 'a1', customers: [ 'c1', 'c2' ] } ), From ce84027ae996923432129bc1e71b66e778e62e3e Mon Sep 17 00:00:00 2001 From: Phil Freo Date: Tue, 9 Jul 2013 17:57:52 -0700 Subject: [PATCH 2/4] Fix for #375 - toJSON should include IDs of unregistered models Side effect (seems fine to me) is that it also continues to return IDs of deleted models, which seems less magic and more expected to me - also allows for 'soft deletes' where an object itself is deleted but other objects can still have references to it. --- backbone-relational.js | 1 + test/tests.js | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/backbone-relational.js b/backbone-relational.js index 044b214c..241d42c6 100755 --- a/backbone-relational.js +++ b/backbone-relational.js @@ -1486,6 +1486,7 @@ } else if ( rel instanceof Backbone.HasOne ) { value = value || rel.keyId; + if (!value && _.isString(rel.keyContents)) value = rel.keyContents; } } } diff --git a/test/tests.js b/test/tests.js index a0ca61c8..d223876b 100644 --- a/test/tests.js +++ b/test/tests.js @@ -1113,7 +1113,7 @@ $(document).ready(function() { u1.destroy(); personJSON = person.toJSON(); ok( !u1.get( 'person' ) ); - equal( personJSON.user_id, null, "`user_id` does not get set in JSON anymore" ); + equal( personJSON.user_id, 'u1', "`user_id` still gets set in JSON" ); person = new Person({user : new User({ id : 'u2' })}) equal(person.toJSON().user_id, 'u2') @@ -1121,6 +1121,25 @@ $(document).ready(function() { equal(person.toJSON().user_id, 'unfetched_user_id') }); + test( "`toJSON` should include ids for unregistered models (if `includeInJSON` is `idAttribute`)", function() { + + // Person -> User; `idAttribute` on a HasOne + var person = new Person({ id: 'p1', user: 'u1' } ), + personJSON = person.toJSON(); + + equal( personJSON.user_id, 'u1', "`user_id` gets set in JSON even though no user obj exists" ); + + var u1 = new User( { id: 'u1' } ); + personJSON = person.toJSON(); + ok( u1.get( 'person' ) === person ); + equal( personJSON.user_id, 'u1', "`user_id` gets set in JSON after matching user obj is created" ); + + Backbone.Relational.store.unregister(u1); + + personJSON = person.toJSON(); + equal( personJSON.user_id, 'u1', "`user_id` gets set in JSON after user was unregistered from store" ); + }); + test( "`parse` gets called through `findOrCreate`", function() { var parseCalled = 0; Zoo.prototype.parse = Animal.prototype.parse = function( resp, options ) { From 76bf29629bb421040d439418a56258f70fa5ee9b Mon Sep 17 00:00:00 2001 From: Phil Freo Date: Wed, 10 Jul 2013 15:05:54 -0700 Subject: [PATCH 3/4] Fixup for numerical IDs --- backbone-relational.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone-relational.js b/backbone-relational.js index 241d42c6..162534d7 100755 --- a/backbone-relational.js +++ b/backbone-relational.js @@ -1486,7 +1486,7 @@ } else if ( rel instanceof Backbone.HasOne ) { value = value || rel.keyId; - if (!value && _.isString(rel.keyContents)) value = rel.keyContents; + if (!value && !_.isObject(rel.keyContents)) value = rel.keyContents; } } } From 54e5901c4d7479ecda9004defc98e2cd817f8f5e Mon Sep 17 00:00:00 2001 From: Phil Freo Date: Thu, 18 Jul 2013 11:06:35 -0700 Subject: [PATCH 4/4] return null rather than undefined --- backbone-relational.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone-relational.js b/backbone-relational.js index 162534d7..545a3251 100755 --- a/backbone-relational.js +++ b/backbone-relational.js @@ -1486,7 +1486,7 @@ } else if ( rel instanceof Backbone.HasOne ) { value = value || rel.keyId; - if (!value && !_.isObject(rel.keyContents)) value = rel.keyContents; + if (!value && !_.isObject(rel.keyContents)) value = rel.keyContents || null; } } }