From eefb920d0e0345485a8eb120aeecc3b1aa9f6719 Mon Sep 17 00:00:00 2001 From: Alkis Evlogimenos Date: Wed, 22 Sep 2010 09:11:36 +0200 Subject: 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() --- src/Resource.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/Resource.js b/src/Resource.js index 09c09acc..bab1b800 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -88,7 +88,7 @@ ResourceFactory.prototype = { 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( action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), @@ -118,8 +118,7 @@ ResourceFactory.prototype = { }; Resource.prototype['$' + name] = function(a1, a2){ - var self = this; - var params = extractParams(self); + var params = extractParams(this); var callback = noop; switch(arguments.length) { case 2: params = a1; callback = a2; @@ -128,11 +127,8 @@ ResourceFactory.prototype = { 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); - }); + var data = isPostOrPut ? this : _undefined; + Resource[name].call(this, params, data, callback); }; }); return Resource; -- cgit v1.2.3