From c3d7fd0f1c2ea1724e4399f7d101d3c64f7a1802 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 24 Oct 2013 20:37:43 -0700 Subject: [PATCH] Create remote functions for predefined scopes/relations --- lib/scope.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/scope.js b/lib/scope.js index 50d4b481..e65f748a 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -20,6 +20,7 @@ function defineScope(cls, targetClass, name, params, methods) { } } + // Define a property for the scope Object.defineProperty(cls, name, { enumerable: false, configurable: true, @@ -80,6 +81,44 @@ function defineScope(cls, targetClass, name, params, methods) { } }); + // Wrap the property into a function for remoting + var fn = function() { + var f = this[name]; + f.apply(this, arguments); + }; + + fn.shared = true; + fn.http = {verb: 'get', path: '/' + name}; + fn.accepts = {arg: 'where', type: 'object'}; + fn.description = 'Fetches ' + name; + fn.returns = {arg: name, type: 'array', root: true}; + + cls['__get__' + name] = fn; + + var fn_create = function() { + var f = this[name].create; + f.apply(this, arguments); + }; + + fn_create.shared = true; + fn_create.http = {verb: 'post', path: '/' + name}; + fn_create.accepts = {arg: 'data', type: 'object', source: 'body'}; + fn_create.description = 'Creates ' + name; + fn_create.returns = {arg: 'data', type: 'object', root: true}; + + cls['__create__' + name] = fn_create; + + var fn_delete = function() { + var f = this[name].destroyAll; + f.apply(this, arguments); + }; + fn_delete.shared = true; + fn_delete.http = {verb: 'delete', path: '/' + name}; + fn_delete.description = 'Deletes ' + name; + fn_delete.returns = {arg: 'data', type: 'object', root: true}; + + cls['__delete__' + name] = fn_delete; + // and it should have create/build methods with binded thisModelNameId param function build(data) { return new targetClass(mergeParams(this._scope, {where:data || {}}).where);