Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 293f34c

Browse files
alkismhevery
authored andcommitted
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.
1 parent b798ee8 commit 293f34c

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/Resource.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ ResourceFactory.prototype = {
6363
}
6464

6565
foreach(actions, function(action, name){
66-
var isGet = action.method == 'GET';
6766
var isPostOrPut = action.method == 'POST' || action.method == 'PUT';
6867
Resource[name] = function (a1, a2, a3) {
6968
var params = {};
@@ -118,25 +117,23 @@ ResourceFactory.prototype = {
118117
return self.route(url, extend({}, paramDefaults, additionalParamDefaults), actions);
119118
};
120119

121-
if (!isGet) {
122-
Resource.prototype['$' + name] = function(a1, a2){
123-
var self = this;
124-
var params = extractParams(self);
125-
var callback = noop;
126-
switch(arguments.length) {
127-
case 2: params = a1; callback = a2;
128-
case 1: if (typeof a1 == $function) callback = a1; else params = a1;
129-
case 0: break;
130-
default:
131-
throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments.";
132-
}
133-
var data = isPostOrPut ? self : _undefined;
134-
Resource[name](params, data, function(response){
135-
copy(response, self);
136-
callback(self);
137-
});
138-
};
139-
}
120+
Resource.prototype['$' + name] = function(a1, a2){
121+
var self = this;
122+
var params = extractParams(self);
123+
var callback = noop;
124+
switch(arguments.length) {
125+
case 2: params = a1; callback = a2;
126+
case 1: if (typeof a1 == $function) callback = a1; else params = a1;
127+
case 0: break;
128+
default:
129+
throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments.";
130+
}
131+
var data = isPostOrPut ? self : _undefined;
132+
Resource[name](params, data, function(response){
133+
copy(response, self);
134+
callback(self);
135+
});
136+
};
140137
});
141138
return Resource;
142139
}

test/ResourceSpec.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ describe("resource", function() {
7070
expect(callback).wasCalledWith(cc);
7171
});
7272

73+
it("should read partial resource", function(){
74+
xhr.expectGET("/CreditCard").respond([{id:{key:123}}]);
75+
xhr.expectGET("/CreditCard/123").respond({id:{key:123}, number:'9876'});
76+
var ccs = CreditCard.query();
77+
xhr.flush();
78+
expect(ccs.length).toEqual(1);
79+
var cc = ccs[0];
80+
expect(cc instanceof CreditCard).toBeTruthy();
81+
expect(cc.number).not.toBeDefined();
82+
cc.$get(callback);
83+
xhr.flush();
84+
expect(callback).wasCalledWith(cc);
85+
expect(cc.number).toEqual('9876');
86+
});
87+
7388
it("should update resource", function(){
7489
xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});
7590

@@ -124,8 +139,8 @@ describe("resource", function() {
124139
it('should create on save', function(){
125140
xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123});
126141
var cc = new CreditCard();
127-
expect(cc.$get).not.toBeDefined();
128-
expect(cc.$query).not.toBeDefined();
142+
expect(cc.$get).toBeDefined();
143+
expect(cc.$query).toBeDefined();
129144
expect(cc.$remove).toBeDefined();
130145
expect(cc.$save).toBeDefined();
131146

0 commit comments

Comments
 (0)