Skip to content

Commit

Permalink
Merge pull request #105 from DouweM/toJSON-for-null
Browse files Browse the repository at this point in the history
NOT BACKWARDS-COMPATIBLE: Serialize `null` relations as `null` in #toJSON().
  • Loading branch information
PaulUithol committed Apr 23, 2012
2 parents 02474c1 + f55fdc0 commit 2b1c10b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 11 additions & 3 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@
},

_getCollectionOptions: function() {
return _.isFunction( this.options.collectionOptions ) ?
return _.isFunction( this.options.collectionOptions ) ?
this.options.collectionOptions( this.instance ) :
this.options.collectionOptions;
},
Expand Down Expand Up @@ -1248,8 +1248,13 @@
_.each( this._relations, function( rel ) {
var value = json[ rel.key ];

if ( rel.options.includeInJSON === true && value && _.isFunction( value.toJSON ) ) {
json[ rel.keyDestination ] = value.toJSON();
if ( rel.options.includeInJSON === true) {
if ( value && _.isFunction( value.toJSON ) ) {
json[ rel.keyDestination ] = value.toJSON();
}
else {
json[ rel.keyDestination ] = null;
}
}
else if ( _.isString( rel.options.includeInJSON ) ) {
if ( value instanceof Backbone.Collection ) {
Expand All @@ -1258,6 +1263,9 @@
else if ( value instanceof Backbone.Model ) {
json[ rel.keyDestination ] = value.get( rel.options.includeInJSON );
}
else {
json[ rel.keyDestination ] = null;
}
}
else {
delete json[ rel.key ];
Expand Down
17 changes: 11 additions & 6 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ $(document).ready(function() {
model: Animal,

initialize: function( models, options ) {
options || (options = {});
this.url = options.url;
options || (options = {});
this.url = options.url;
}
});

Expand Down Expand Up @@ -123,6 +123,7 @@ $(document).ready(function() {
{
type: Backbone.HasOne,
key: 'user',
keyDestination: 'user_id',
relatedModel: 'User',
includeInJSON: Backbone.Model.prototype.idAttribute,
reverseRelation: {
Expand Down Expand Up @@ -611,7 +612,7 @@ $(document).ready(function() {

test( "'includeInJSON' (Person to JSON)", function() {
var json = person1.toJSON();
equal( json.user, 'user-1', "The value 'user' is the user's id (not an object, since 'includeInJSON' is set to the idAttribute)" );
equal( json.user_id, 'user-1', "The value of 'user_id' is the user's id (not an object, since 'includeInJSON' is set to the idAttribute)" );
ok ( json.likesALot instanceof Object, "The value of 'likesALot' is an object ('includeInJSON' is 'true')" );
equal( json.likesALot.likesALot, 'person-1', "Person is serialized only once" );

Expand All @@ -621,6 +622,10 @@ $(document).ready(function() {
json = person2.toJSON();
ok( person2.get('livesIn') instanceof House, "'person2' has a 'livesIn' relation" );
equal( json.livesIn, undefined , "The value of 'livesIn' is not serialized ('includeInJSON is 'false')" );

json = person3.toJSON();
ok( json.user_id === null, "The value of 'user_id' is null");
ok( json.likesALot === null, "The value of 'likesALot' is null");
});

test( "'createModels' is false", function() {
Expand Down Expand Up @@ -734,9 +739,9 @@ $(document).ready(function() {
ok( typeof viewJSON.properties === 'undefined', "'viewJSON' does not have 'properties'" );
});

test( "'collectionOptionsCallback' sets the options on the created HasMany Collections", function() {
var zoo = new Zoo();
ok( zoo.get("animals").url === "zoo/" + zoo.cid + "/animal/");
test( "'collectionOptions' sets the options on the created HasMany Collections", function() {
var zoo = new Zoo();
ok( zoo.get("animals").url === "zoo/" + zoo.cid + "/animal/");
});


Expand Down

0 comments on commit 2b1c10b

Please sign in to comment.