Merge pull request #56 from strongloop/feature/fix-relation-remoting
Fix the remoting method with the current receiver (this)
This commit is contained in:
commit
a9720d4b08
|
@ -26,7 +26,7 @@ Relation.relationNameFor = function relationNameFor(foreignKey) {
|
||||||
* @example `User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});`
|
* @example `User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});`
|
||||||
*/
|
*/
|
||||||
Relation.hasMany = function hasMany(anotherClass, params) {
|
Relation.hasMany = function hasMany(anotherClass, params) {
|
||||||
var thisClass = this, thisClassName = this.modelName;
|
var thisClassName = this.modelName;
|
||||||
params = params || {};
|
params = params || {};
|
||||||
if (typeof anotherClass === 'string') {
|
if (typeof anotherClass === 'string') {
|
||||||
params.as = anotherClass;
|
params.as = anotherClass;
|
||||||
|
@ -71,7 +71,6 @@ Relation.hasMany = function hasMany(anotherClass, params) {
|
||||||
done = function() {};
|
done = function() {};
|
||||||
}
|
}
|
||||||
var self = this;
|
var self = this;
|
||||||
var id = this[idName];
|
|
||||||
anotherClass.create(data, function(err, ac) {
|
anotherClass.create(data, function(err, ac) {
|
||||||
if (err) return done(err, ac);
|
if (err) return done(err, ac);
|
||||||
var d = {};
|
var d = {};
|
||||||
|
@ -98,7 +97,6 @@ Relation.hasMany = function hasMany(anotherClass, params) {
|
||||||
params.through.findOrCreate({where: query}, data, done);
|
params.through.findOrCreate({where: query}, data, done);
|
||||||
};
|
};
|
||||||
scopeMethods.remove = function(acInst, done) {
|
scopeMethods.remove = function(acInst, done) {
|
||||||
var self = this;
|
|
||||||
var q = {};
|
var q = {};
|
||||||
q[fk2] = acInst[idName] || acInst;
|
q[fk2] = acInst[idName] || acInst;
|
||||||
params.through.findOne({where: q}, function(err, d) {
|
params.through.findOne({where: q}, function(err, d) {
|
||||||
|
|
22
lib/scope.js
22
lib/scope.js
|
@ -24,6 +24,17 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
Object.defineProperty(cls, name, {
|
Object.defineProperty(cls, name, {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
|
/**
|
||||||
|
* This defines a property for the scope. For example, user.accounts or
|
||||||
|
* User.vips. Please note the cls can be the model class or prototype of
|
||||||
|
* the model class.
|
||||||
|
*
|
||||||
|
* The property value is function. It can be used to query the scope,
|
||||||
|
* such as user.accounts(condOrRefresh, cb) or User.vips(cb). The value
|
||||||
|
* can also have child properties for create/build/delete. For example,
|
||||||
|
* user.accounts.create(act, cb).
|
||||||
|
*
|
||||||
|
*/
|
||||||
get: function () {
|
get: function () {
|
||||||
var f = function caller(condOrRefresh, cb) {
|
var f = function caller(condOrRefresh, cb) {
|
||||||
var actualCond = {};
|
var actualCond = {};
|
||||||
|
@ -60,6 +71,7 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
f._scope = typeof params === 'function' ? params.call(this) : params;
|
f._scope = typeof params === 'function' ? params.call(this) : params;
|
||||||
|
|
||||||
f.build = build;
|
f.build = build;
|
||||||
f.create = create;
|
f.create = create;
|
||||||
f.destroyAll = destroyAll;
|
f.destroyAll = destroyAll;
|
||||||
|
@ -83,8 +95,10 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
|
|
||||||
// Wrap the property into a function for remoting
|
// Wrap the property into a function for remoting
|
||||||
var fn = function() {
|
var fn = function() {
|
||||||
|
// primaryObject.scopeName, such as user.accounts
|
||||||
var f = this[name];
|
var f = this[name];
|
||||||
f.apply(this, arguments);
|
// set receiver to be the scope property whose value is a function
|
||||||
|
f.apply(this[name], arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
fn.shared = true;
|
fn.shared = true;
|
||||||
|
@ -97,12 +111,12 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
|
|
||||||
var fn_create = function() {
|
var fn_create = function() {
|
||||||
var f = this[name].create;
|
var f = this[name].create;
|
||||||
f.apply(this, arguments);
|
f.apply(this[name], arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
fn_create.shared = true;
|
fn_create.shared = true;
|
||||||
fn_create.http = {verb: 'post', path: '/' + name};
|
fn_create.http = {verb: 'post', path: '/' + name};
|
||||||
fn_create.accepts = {arg: 'data', type: 'object', source: 'body'};
|
fn_create.accepts = {arg: 'data', type: 'object', http: {source: 'body'}};
|
||||||
fn_create.description = 'Creates ' + name;
|
fn_create.description = 'Creates ' + name;
|
||||||
fn_create.returns = {arg: 'data', type: 'object', root: true};
|
fn_create.returns = {arg: 'data', type: 'object', root: true};
|
||||||
|
|
||||||
|
@ -110,7 +124,7 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
|
|
||||||
var fn_delete = function() {
|
var fn_delete = function() {
|
||||||
var f = this[name].destroyAll;
|
var f = this[name].destroyAll;
|
||||||
f.apply(this, arguments);
|
f.apply(this[name], arguments);
|
||||||
};
|
};
|
||||||
fn_delete.shared = true;
|
fn_delete.shared = true;
|
||||||
fn_delete.http = {verb: 'delete', path: '/' + name};
|
fn_delete.http = {verb: 'delete', path: '/' + name};
|
||||||
|
|
Loading…
Reference in New Issue