Skip to content

Commit

Permalink
Merge pull request #435 from adambiggs/assign-record-ids-first
Browse files Browse the repository at this point in the history
Always assign record IDs before other attributes in Spine.Model::load()
  • Loading branch information
aeischeid committed Mar 19, 2013
2 parents a042a3f + 1679f05 commit f1e1e06
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/spine.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/spine.coffee
Expand Up @@ -254,7 +254,8 @@ class Model extends Module
validate: ->

load: (atts) ->
for key, value of atts
if atts.id then @id = atts.id
for own key, value of atts
if typeof @[key] is 'function'
@[key](value)
else
Expand Down
87 changes: 79 additions & 8 deletions test/specs/model.relation.js
Expand Up @@ -37,32 +37,103 @@ describe("Model.Relation", function(){
expect( photo.album().exists() ).toEqual(true);
});

it("should load nested Singleton record", function(){
it("should associate an existing Singleton record", function(){
Album.hasOne("photo", Photo);
Photo.belongsTo("album", Album);

var album = new Album();
album.load({id: "1", name: "Beautiful album",
photo: {id: "2", name: "Beautiful photo", album_id: "1"}});
album.load({
id: "1",
name: "Beautiful album",
});

var photo = new Photo();
photo.load({
id: "2",
name: "Beautiful photo"
});

album.photo(photo);

expect( album.photo() ).toBeTruthy();
expect( album.photo().album_id ).toBe("1");
expect( album.photo().name ).toBe("Beautiful photo");
});

it("should load nested Collection records", function(){
it("should create a new related Singleton record", function(){
Album.hasOne("photo", Photo);
Photo.belongsTo("album", Album);

var album = new Album();
album.load({
name: "Beautiful album",
photo: {
name: "Beautiful photo"
},
id: "1"
});

expect( album.photo() ).toBeTruthy();
expect( album.photo().album_id ).toBe("1");
expect( album.photo().name ).toBe("Beautiful photo");
});

it("should associate existing Collection records", function(){
Album.hasMany("photos", Photo);
Photo.belongsTo("album", Album);

var album = new Album();
album.load({
id: "1", name: "Beautiful album",
photos: [{id: "1", name: "Beautiful photo 1", album_id: "1"},
{id: "2", name: "Beautiful photo 2", album_id: "1"}]
});
name: "Beautiful album",
id: "1"
});

var photo1 = new Photo();
photo1.load({
id: "1",
name: "Beautiful photo 1"
});

var photo2 = new Photo();
photo2.load({
id: "2",
name: "Beautiful photo 2"
});

album.photos([ photo1, photo2 ]);

expect( album.photos() ).toBeTruthy();
expect( album.photos().all().length ).toBe(2);
expect( album.photos().first().album_id ).toBe("1");
expect( album.photos().last().album_id ).toBe("1");
expect( album.photos().first().name ).toBe("Beautiful photo 1");
expect( album.photos().last().name ).toBe("Beautiful photo 2");
});

it("should create new related Collection records", function(){
Album.hasMany("photos", Photo);
Photo.belongsTo("album", Album);

var album = new Album();
album.load({
name: "Beautiful album",
photos: [{
id: "1",
name: "Beautiful photo 1"
},
{
id: "2",
name: "Beautiful photo 2"
}],
id: "1"
});

expect( album.photos() ).toBeTruthy();
expect( album.photos().all().length ).toBe(2);
expect( album.photos().first().album_id ).toBe("1");
expect( album.photos().last().album_id ).toBe("1");
expect( album.photos().first().name ).toBe("Beautiful photo 1");
expect( album.photos().last().name ).toBe("Beautiful photo 2");
});

});

0 comments on commit f1e1e06

Please sign in to comment.