From 4b868c6284e85b9910a57d57f1e1931269f48724 Mon Sep 17 00:00:00 2001 From: Phil Freo Date: Thu, 4 Apr 2013 13:42:34 -0700 Subject: [PATCH] Two tests showing Collection reset/set should both link related objects properly --- test/tests.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/tests.js b/test/tests.js index 762e929e..1c888450 100644 --- a/test/tests.js +++ b/test/tests.js @@ -297,9 +297,30 @@ $(document).ready(function() { } }); + window.Country = Backbone.RelationalModel.extend(); + window.CountryCollection = Backbone.Collection.extend({ + model: Country + }); + window.Address = Backbone.RelationalModel.extend({ urlRoot: '/address/', + relations: [{ + type: Backbone.HasOne, + key: 'country', + keySource: 'country_id', + relatedModel: 'Country' + } + ], + + initialize: function(attributes, options) { + // initialize an empty object if we don't already have one + // as an alternative to fetchRelated() + if ( attributes.country_id && !this.get('country') ) { + this.set('country', Country.findOrCreate( { id: attributes.country_id } )); + } + }, + toString: function() { return 'Address (' + this.id + ')'; } @@ -1238,6 +1259,48 @@ $(document).ready(function() { ok( contact === contacts.first(), '... and same model instances' ); }); + test( "object gets related object linked properly when loaded using Collection#set", function() { + var countries = new CountryCollection(); + + // create address + var address = new Address({ + id: 'address-1', + country_id: 'US' + }); + var country = address.get( 'country' ); + ok( country, 'address country exists' ); + ok( country.id, '... with valid id' ); + ok( !country.has('name'), '... without a name yet' ); + ok( country instanceof Country, '... and is instanceof Country' ); + + // then countries fetch completes... if using SET + countries.set( [ { id: 'US', name: 'United States' } ] ); + var country2 = address.get( 'country' ); + ok( country === country2, 'after reset, address country is the same instance' ); + equal( country.get('name'), 'United States', '... but now it has all the attributes' ); + }); + + test( "object gets related object linked properly when loaded using Collection#reset", function() { + var countries = new CountryCollection(); + + // create address + var address = new Address({ + id: 'address-1', + country_id: 'US' + }); + var country = address.get( 'country' ); + ok( country, 'address country exists' ); + ok( country.id, '... with valid id' ); + ok( !country.has('name'), '... without a name yet' ); + ok( country instanceof Country, '... and is instanceof Country' ); + + // then countries fetch completes... if using RESET + countries.reset( [ { id: 'US', name: 'United States' } ] ); + var country2 = address.get( 'country' ); + ok( country === country2, 'after reset, address country is the same instance' ); + equal( country.get('name'), 'United States', '... but now it has all the attributes' ); + }); + test( "constructor.findOrCreate", function() { var personColl = Backbone.Relational.store.getCollection( person1 ), origPersonCollSize = personColl.length;