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

Added class function find, to allow finding models by primary key #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion spec/javascripts/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('A Resource instance', function() {

it('should not have a URL', function() {
expect(model.resourceURL()).toBeUndefined();
})
});
});

it('allows setting of properties not in the schema during creation', function() {
Expand Down Expand Up @@ -80,4 +80,18 @@ describe('A Resource instance', function() {
});
});

describe('finding objects via find', function() {
beforeEach(function() {
model = Model.create({id: 1});
});

it('should return model for the given primary key if it exists', function() {
expect(Model.find(1)).toBe(model);
});

it('should return null if no model with the given primary key exists', function() {
expect(Model.find(2)).toBeNull();
});
});

});
7 changes: 7 additions & 0 deletions src/sproutcore-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,13 @@
return this;
},

find: function(id) {
if (SC.none(this.identityMap)) {
return null;
}
return this.identityMap[id] || null;
},

// Create an instance of this resource. If `options` includes an
// `id`, first check the identity map and return the existing resource
// with that ID if found.
Expand Down