Skip to content

Commit eefb920

Browse files
alkismhevery
authored andcommitted
Reduce copies done by Resource.
When a method foo is called on a Resource object, say myResource there are two copies that happen to the resource: - one inside Resource.foo() in some dummy function - another inside myResource.$foo() inside the callback passed to foo()
1 parent 006fd2c commit eefb920

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/Resource.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ ResourceFactory.prototype = {
8888
throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments.";
8989
}
9090

91-
var value = action.isArray ? [] : new Resource(data);
91+
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
9292
self.xhr(
9393
action.method,
9494
route.url(extend({}, action.params || {}, extractParams(data), params)),
@@ -118,8 +118,7 @@ ResourceFactory.prototype = {
118118
};
119119

120120
Resource.prototype['$' + name] = function(a1, a2){
121-
var self = this;
122-
var params = extractParams(self);
121+
var params = extractParams(this);
123122
var callback = noop;
124123
switch(arguments.length) {
125124
case 2: params = a1; callback = a2;
@@ -128,11 +127,8 @@ ResourceFactory.prototype = {
128127
default:
129128
throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments.";
130129
}
131-
var data = isPostOrPut ? self : _undefined;
132-
Resource[name](params, data, function(response){
133-
copy(response, self);
134-
callback(self);
135-
});
130+
var data = isPostOrPut ? this : _undefined;
131+
Resource[name].call(this, params, data, callback);
136132
};
137133
});
138134
return Resource;

0 commit comments

Comments
 (0)