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()
This commit is contained in:
Alkis Evlogimenos 2010-09-22 09:11:36 +02:00 committed by Misko Hevery
parent 006fd2ca25
commit eefb920d0e

View file

@ -88,7 +88,7 @@ ResourceFactory.prototype = {
throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments."; throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments.";
} }
var value = action.isArray ? [] : new Resource(data); var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
self.xhr( self.xhr(
action.method, action.method,
route.url(extend({}, action.params || {}, extractParams(data), params)), route.url(extend({}, action.params || {}, extractParams(data), params)),
@ -118,8 +118,7 @@ ResourceFactory.prototype = {
}; };
Resource.prototype['$' + name] = function(a1, a2){ Resource.prototype['$' + name] = function(a1, a2){
var self = this; var params = extractParams(this);
var params = extractParams(self);
var callback = noop; var callback = noop;
switch(arguments.length) { switch(arguments.length) {
case 2: params = a1; callback = a2; case 2: params = a1; callback = a2;
@ -128,11 +127,8 @@ ResourceFactory.prototype = {
default: default:
throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments."; throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments.";
} }
var data = isPostOrPut ? self : _undefined; var data = isPostOrPut ? this : _undefined;
Resource[name](params, data, function(response){ Resource[name].call(this, params, data, callback);
copy(response, self);
callback(self);
});
}; };
}); });
return Resource; return Resource;