diff options
| -rw-r--r-- | src/Resource.js | 37 | ||||
| -rw-r--r-- | test/ResourceSpec.js | 19 |
2 files changed, 34 insertions, 22 deletions
diff --git a/src/Resource.js b/src/Resource.js index 85028dc1..09c09acc 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -63,7 +63,6 @@ ResourceFactory.prototype = { } foreach(actions, function(action, name){ - var isGet = action.method == 'GET'; var isPostOrPut = action.method == 'POST' || action.method == 'PUT'; Resource[name] = function (a1, a2, a3) { var params = {}; @@ -118,25 +117,23 @@ ResourceFactory.prototype = { return self.route(url, extend({}, paramDefaults, additionalParamDefaults), actions); }; - if (!isGet) { - Resource.prototype['$' + name] = function(a1, a2){ - var self = this; - var params = extractParams(self); - var callback = noop; - switch(arguments.length) { - case 2: params = a1; callback = a2; - case 1: if (typeof a1 == $function) callback = a1; else params = a1; - case 0: break; - default: - throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments."; - } - var data = isPostOrPut ? self : _undefined; - Resource[name](params, data, function(response){ - copy(response, self); - callback(self); - }); - }; - } + Resource.prototype['$' + name] = function(a1, a2){ + var self = this; + var params = extractParams(self); + var callback = noop; + switch(arguments.length) { + case 2: params = a1; callback = a2; + case 1: if (typeof a1 == $function) callback = a1; else params = a1; + case 0: break; + default: + throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments."; + } + var data = isPostOrPut ? self : _undefined; + Resource[name](params, data, function(response){ + copy(response, self); + callback(self); + }); + }; }); return Resource; } diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 1ac43d74..435176b0 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -70,6 +70,21 @@ describe("resource", function() { expect(callback).wasCalledWith(cc); }); + it("should read partial resource", function(){ + xhr.expectGET("/CreditCard").respond([{id:{key:123}}]); + xhr.expectGET("/CreditCard/123").respond({id:{key:123}, number:'9876'}); + var ccs = CreditCard.query(); + xhr.flush(); + expect(ccs.length).toEqual(1); + var cc = ccs[0]; + expect(cc instanceof CreditCard).toBeTruthy(); + expect(cc.number).not.toBeDefined(); + cc.$get(callback); + xhr.flush(); + expect(callback).wasCalledWith(cc); + expect(cc.number).toEqual('9876'); + }); + it("should update resource", function(){ xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'}); @@ -124,8 +139,8 @@ describe("resource", function() { it('should create on save', function(){ xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123}); var cc = new CreditCard(); - expect(cc.$get).not.toBeDefined(); - expect(cc.$query).not.toBeDefined(); + expect(cc.$get).toBeDefined(); + expect(cc.$query).toBeDefined(); expect(cc.$remove).toBeDefined(); expect(cc.$save).toBeDefined(); |
