Permalink
Browse files

toJSON: if set to just the id, also add ids for 'unfound' models. Sho…

…uld fix #191.
  • Loading branch information...
1 parent b715776 commit dcaf29975a2b23f85124a060526b5b5f27f11b51 @PaulUithol committed Mar 10, 2013
Showing with 11 additions and 1 deletion.
  1. +10 −0 backbone-relational.js
  2. +1 −1 test/tests.js
View
@@ -1413,6 +1413,16 @@
else if ( related instanceof Backbone.Model ) {
value = related.get( includeInJSON );
}
+
+ // Add 'unfound' ids if only includeInJSON is the `idAttribute`
+ if ( includeInJSON === rel.relatedModel.prototype.idAttribute ) {
+ if ( rel instanceof Backbone.HasMany ) {
+ value = value.concat( rel.keyIds );
+ }
+ else if ( rel instanceof Backbone.HasOne ) {
+ value = value || rel.keyId;
+ }
+ }
}
else if ( _.isArray( includeInJSON ) ) {
if ( related instanceof Backbone.Collection ) {
View
@@ -1023,7 +1023,7 @@ $(document).ready(function() {
ok( _.isArray( agentJSON.customers ) );
equal( agentJSON.customers.length, 2, "2 customers in agentJSON; it serializes the `idAttribute`" );
- var c1 = new Agent( { id: 'c1' } );
+ var c1 = new Customer( { id: 'c1' } );
equal( agent.get( 'customers' ).length, 1, '1 customer in agent' );
agentJSON = agent.toJSON();

0 comments on commit dcaf299

Please sign in to comment.