Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions lib/model-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
var args = slice.call(arguments);
var pluralName = (settings && settings.plural) ||
inflection.pluralize(className);


var httpOptions = (settings && settings.http) || {};
var pathName = httpOptions.path || pluralName;

if (!className) {
throw new Error('Class name required');
}
Expand Down Expand Up @@ -188,7 +191,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
hiddenProperty(ModelClass, 'dataSource', modelBuilder); // Keep for back-compatibility
hiddenProperty(ModelClass, 'pluralModelName', pluralName);
hiddenProperty(ModelClass, 'relations', {});
hiddenProperty(ModelClass, 'http', { path: '/' + pluralName });
hiddenProperty(ModelClass, 'http', { path: '/' + pathName });
hiddenProperty(ModelClass, 'base', ModelBaseClass);

// inherit ModelBaseClass static methods
Expand Down
29 changes: 19 additions & 10 deletions lib/relation-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ function extendScopeMethods(definition, scopeMethods, ext) {
return relationMethod.apply(relation, arguments);
};
if (relationMethod.shared) {
method.shared = true;
method.accepts = relationMethod.accepts;
method.returns = relationMethod.returns;
method.http = relationMethod.http;
method.description = relationMethod.description;
sharedMethod(definition, key, method, relationMethod);
}
customMethods.push(key);
}
Expand Down Expand Up @@ -573,15 +569,19 @@ function scopeMethod(definition, methodName) {

var relationMethod = relationClass.prototype[methodName];
if (relationMethod.shared) {
method.shared = true;
method.accepts = relationMethod.accepts;
method.returns = relationMethod.returns;
method.http = relationMethod.http;
method.description = relationMethod.description;
sharedMethod(definition, methodName, method, relationMethod);
}
return method;
}

function sharedMethod(definition, methodName, method, relationMethod) {
method.shared = true;
method.accepts = relationMethod.accepts;
method.returns = relationMethod.returns;
method.http = relationMethod.http;
method.description = relationMethod.description;
};

/**
* Find a related item by foreign key
* @param {*} fkId The foreign key
Expand Down Expand Up @@ -1295,6 +1295,15 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
return relationMethod;
}
});

// FIXME: [rfeng] Wrap the property into a function for remoting
// so that it can be accessed as /api/<model>/<id>/<hasOneRelationName>
// For example, /api/orders/1/customer
var fn = function() {
var f = this[relationName];
f.apply(this, arguments);
};
modelFrom.prototype['__get__' + relationName] = fn;
};

/**
Expand Down
9 changes: 9 additions & 0 deletions test/loopback-dl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,15 @@ describe('DataSource define model', function () {

done();
});

it('should allow an explicit remoting path', function () {
var ds = new DataSource('memory');

var User = ds.define('User', {name: String, bio: String}, {
http: { path: 'accounts' }
});
User.http.path.should.equal('/accounts');
});

});

Expand Down