aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlkis Evlogimenos2010-09-15 19:27:58 +0200
committerMisko Hevery2010-09-16 00:23:22 +0200
commit293f34cd64886a1dddae9c295fafde5c47029a3b (patch)
treeb1cb99e8be24eb23d39ac8aee45d531e3ee96704
parentb798ee80c29d877aa8c9934382a140a080fa13bc (diff)
downloadangular.js-293f34cd64886a1dddae9c295fafde5c47029a3b.tar.bz2
Expose GET operations on resources as well. This allows us to read
"partials". The pattern is demostrated in the unittest: Resource.query returns a list of "keys" to resources, which are partially defined. They have enough data to allow $get to fetch the whole gamout. Then $get fetches all the details of the resource.
-rw-r--r--src/Resource.js37
-rw-r--r--test/ResourceSpec.js19
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();