New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #375 - toJSON should include IDs of unregistered models #376

Merged
merged 4 commits into from Oct 29, 2013
Jump to file or symbol
Failed to load files and symbols.
+21 −1
Diff settings

Always

Just for now

Viewing a subset of changes. View all

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.
  • Loading branch information...
philfreo committed Jul 10, 2013
commit ce84027ae996923432129bc1e71b66e778e62e3e
View
@@ -1486,6 +1486,7 @@
}
else if ( rel instanceof Backbone.HasOne ) {
value = value || rel.keyId;
if (!value && _.isString(rel.keyContents)) value = rel.keyContents;
}
}
}
View
@@ -1113,14 +1113,33 @@ $(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')
person.set({user : 'unfetched_user_id'})
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 ) {
ProTip! Use n and p to navigate between commits in a pull request.