diff --git a/spec/javascripts/resourceSpec.js b/spec/javascripts/resourceSpec.js index ae65af8..e18a0c5 100644 --- a/spec/javascripts/resourceSpec.js +++ b/spec/javascripts/resourceSpec.js @@ -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() { @@ -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(); + }); + }); + }); diff --git a/src/sproutcore-resource.js b/src/sproutcore-resource.js index f546088..24f6d63 100644 --- a/src/sproutcore-resource.js +++ b/src/sproutcore-resource.js @@ -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.