Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 2 additions & 46 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,54 +94,10 @@ loopback.createDataSource = function (name, options) {
ds.createModel = function (name, properties, settings) {
var ModelCtor = loopback.createModel(name, properties, settings);
ModelCtor.attachTo(ds);

var hasMany = ModelCtor.hasMany;

if(hasMany) {
ModelCtor.hasMany = function (anotherClass, params) {
var origArgs = arguments;
var thisClass = this, thisClassName = this.modelName;
params = params || {};
if (typeof anotherClass === 'string') {
params.as = anotherClass;
if (params.model) {
anotherClass = params.model;
} else {
var anotherClassName = i8n.singularize(anotherClass).toLowerCase();
for(var name in this.schema.models) {
if (name.toLowerCase() === anotherClassName) {
anotherClass = this.schema.models[name];
}
}
}
}

var pluralized = anotherClass.pluralModelName || i8n.pluralize(anotherClass.modelName);
var methodName = params.as || i8n.camelize(i8n.pluralize(anotherClass.modelName), true);
var proxyMethodName = 'get' + i8n.titleize(pluralized, true);

// create a proxy method
var fn = this.prototype[proxyMethodName] = function () {
// this[methodName] cannot be a shared method
// because it is defined inside
// a property getter...

var f = (this[methodName] || this[pluralized]);
f.apply(thisClass, arguments);
};

fn.shared = true;
fn.http = {verb: 'get', path: '/' + pluralized};
fn.accepts = {arg: 'where', type: 'object'};
fn.returns = {root: true};
hasMany.apply(this, arguments);
};
}

return ModelCtor;
}
};
return ds;
}
};

/**
* Create a named vanilla JavaScript class constructor with an attached set of properties and options.
Expand Down