Create remote functions for predefined scopes/relations
This commit is contained in:
parent
f164ed6919
commit
c3d7fd0f1c
39
lib/scope.js
39
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);
|
||||
|
|
Loading…
Reference in New Issue