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.
This commit is contained in:
Alkis Evlogimenos 2010-09-15 19:27:58 +02:00 committed by Misko Hevery
parent b798ee80c2
commit 293f34cd64
2 changed files with 34 additions and 22 deletions

View file

@ -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;
}

View file

@ -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();