Skip to content
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

Two tests (1 failing) showing Collection reset/set should both link related objects #320

Merged
merged 1 commit into from
Apr 9, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ')';
}
Expand Down Expand Up @@ -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;
Expand Down