Customize/Normalize class-level remoting http path

This commit is contained in:
Fabien Franzen 2014-08-01 09:38:25 +02:00
parent 1247c449b2
commit 71bf0f8240
2 changed files with 34 additions and 2 deletions

View File

@ -111,6 +111,16 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
var pluralName = (settings && settings.plural) ||
inflection.pluralize(className);
var pathName = (settings && settings.path) || pluralName;
var normalize = ModelBuilder.normalizePathName === true || (settings && settings.normalize);
if (this.normalizePathName || (normalize && this.normalizePathName !== false)) {
pathName = inflection.transform(pathName, ['underscore', 'dasherize']);
} else if (typeof ModelBuilder.normalizePathName === 'function' &&
this.normalizePathName !== false) {
pathName = ModelBuilder.normalizePathName.apply(this, [pathName].concat(args));
}
if (!className) {
throw new Error('Class name required');
}
@ -188,7 +198,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

View File

@ -549,6 +549,28 @@ 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}, { path: 'accounts' });
User.http.path.should.equal('/accounts');
});
it('should normalize the remoting path - option', function () {
var ds = new DataSource('memory');
var User = ds.define('UserAccount', {name: String, bio: String}, { normalize: true });
User.http.path.should.equal('/user-accounts');
});
it('should normalize the remoting path - modelBuilder', function () {
var ds = new DataSource('memory');
ds.modelBuilder.normalizePathName = true;
var User = ds.define('UserAccount', {name: String, bio: String});
User.http.path.should.equal('/user-accounts');
});
});
describe('Load models with base', function () {