In progress: rework remoting meta-data

This commit is contained in:
Ritchie Martori 2014-05-16 12:32:54 -07:00
parent 4d5e7884c2
commit 558ea60da0
2 changed files with 27 additions and 39 deletions

View File

@ -51,42 +51,9 @@ DataModel.setup = function setupDataModel() {
DataModel.enableChangeTracking();
});
}
DataModel.on('dataSourceAttached', function() {
DataModel.setupRemoting();
}
/*!
* Configure the remoting attributes for a given function
* @param {Function} fn The function
* @param {Object} options The options
* @private
*/
function setRemoting(target, name, options) {
var fn = target[name];
setupFunction(fn, options);
target[name] = createProxy(fn, options);
}
function createProxy(fn, options) {
var p = function proxy() {
return fn.apply(this, arguments);
}
return setupFunction(fn, options);
}
function setupFunction(fn, options) {
options = options || {};
for (var opt in options) {
if (options.hasOwnProperty(opt)) {
fn[opt] = options[opt];
}
}
fn.shared = true;
// allow connectors to override the function by marking as delegate
fn._delegate = true;
return fn;
});
}
/*!
@ -438,7 +405,11 @@ DataModel.setupRemoting = function() {
var typeName = DataModel.modelName;
var options = DataModel.settings;
// TODO(ritch) setRemoting should create its own function...
function setRemoting(target, name, options) {
var fn = target[name];
options.prototype = target === DataModel.prototype;
DataModel.remoteMethod(name, options);
}
setRemoting(DataModel, 'create', {
description: 'Create a new instance of the model and persist it into the data source',

View File

@ -82,8 +82,6 @@ var _ = require('underscore');
var Model = module.exports = modeler.define('Model');
Model.shared = true;
/*!
* Called when a model is extended.
*/
@ -92,6 +90,8 @@ Model.setup = function () {
var ModelCtor = this;
var options = this.settings;
ModelCtor.sharedClass = new SharedClass(remoteNamespace, ModelCtor);
ModelCtor.sharedCtor = function (data, id, fn) {
if(typeof data === 'function') {
fn = data;
@ -290,5 +290,22 @@ Model.getApp = function(callback) {
}
}
/**
* Enable remote invocation for the method with the given name.
*
* @param {String} name The name of the method.
* ```js
* // static method example (eg. Model.myMethod())
* Model.remoteMethod('myMethod');
* // instance method exampe (eg. Model.prototype.myMethod())
* Model.remoteMethod('prototype.myMethod', {prototype: true});
* @param {Object} options The remoting options.
* See [loopback.remoteMethod()](http://docs.strongloop.com/display/DOC/Remote+methods+and+hooks#Remotemethodsandhooks-loopback.remoteMethod(fn,[options])) for details.
*/
Model.remoteMethod = function(name, options) {
this.sharedClass.defineMethod(name, options);
}
// setup the initial model
Model.setup();