diff --git a/lib/relations.js b/lib/relations.js index 37ec06a9..dca6170a 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -347,14 +347,6 @@ Relation.belongsTo = function (anotherClass, params) { // Call the relation method on the declaring model instance return relationMethod.apply(this, arguments); } - // Set the remoting metadata so that it can be accessed as /api/// - // For example, /api/orders/1/customer - fn.shared = true; - fn.http = {verb: 'get', path: '/' + methodName}; - fn.accepts = {arg: 'refresh', type: 'boolean', http: {source: 'query'}}; - fn.description = 'Fetches belongsTo relation ' + methodName; - fn.returns = {arg: methodName, type: 'object', root: true}; - // Create an instance of the target model and set the foreign key of the // declaring model instance to the id of the target instance fn.create = function(targetModelData, cb) { @@ -374,9 +366,26 @@ Relation.belongsTo = function (anotherClass, params) { return new anotherClass(targetModelData); }.bind(this); + fn._targetClass = anotherClass.modelName; + return fn; }}); + // Wrap the property into a function for remoting + // so that it can be accessed as /api/// + // For example, /api/orders/1/customer + var fn = function() { + var f = this[methodName]; + f.apply(this, arguments); + }; + + fn.shared = true; + fn.http = {verb: 'get', path: '/' + methodName}; + fn.accepts = {arg: 'refresh', type: 'boolean', http: {source: 'query'}}; + fn.description = 'Fetches belongsTo relation ' + methodName; + fn.returns = {arg: methodName, type: 'object', root: true}; + + this.prototype['__get__' + methodName] = fn; }; /**