From 71bf0f82400b2fe7f6c22e723ab89fd6ad084a70 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Fri, 1 Aug 2014 09:38:25 +0200 Subject: [PATCH 1/4] Customize/Normalize class-level remoting http path --- lib/model-builder.js | 14 ++++++++++++-- test/loopback-dl.test.js | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/model-builder.js b/lib/model-builder.js index a688004d..7d8c1770 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -110,7 +110,17 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett var args = slice.call(arguments); 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 diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index a6f840c3..0e1244eb 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -548,6 +548,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'); + }); }); From ef65ffee48ed96e93296f336a147ed4b9ad7c2ff Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Fri, 1 Aug 2014 11:24:41 +0200 Subject: [PATCH 2/4] Changed normalization api - enabled hasOne remoting --- lib/model-builder.js | 11 ++++++----- lib/relation-definition.js | 29 +++++++++++++++++++---------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/model-builder.js b/lib/model-builder.js index 7d8c1770..490b1948 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -53,6 +53,10 @@ function isModelClass(cls) { return cls.prototype instanceof DefaultModelBaseClass; } +ModelBuilder.normalizePathName = function(pathName, className) { + return inflection.transform(pathName, ['underscore', 'dasherize']); +}; + /** * Get a model by name. * @@ -112,13 +116,10 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett inflection.pluralize(className); var pathName = (settings && settings.path) || pluralName; - var normalize = ModelBuilder.normalizePathName === true || (settings && settings.normalize); + var normalize = 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)); + pathName = ModelBuilder.normalizePathName(pathName, className); } if (!className) { diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 44532273..bace4705 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -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); } @@ -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 @@ -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/// + // For example, /api/orders/1/customer + var fn = function() { + var f = this[relationName]; + f.apply(this, arguments); + }; + modelFrom.prototype['__get__' + relationName] = fn; }; /** From 81a822524d7bf35d8431e6bbe6d76c054d04e3a1 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Mon, 4 Aug 2014 12:17:06 +0200 Subject: [PATCH 3/4] Removed normalization (see strong-remoting) --- lib/model-builder.js | 9 --------- test/loopback-dl.test.js | 15 --------------- 2 files changed, 24 deletions(-) diff --git a/lib/model-builder.js b/lib/model-builder.js index 490b1948..3aca2861 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -53,10 +53,6 @@ function isModelClass(cls) { return cls.prototype instanceof DefaultModelBaseClass; } -ModelBuilder.normalizePathName = function(pathName, className) { - return inflection.transform(pathName, ['underscore', 'dasherize']); -}; - /** * Get a model by name. * @@ -116,11 +112,6 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett inflection.pluralize(className); var pathName = (settings && settings.path) || pluralName; - var normalize = settings && settings.normalize; - - if (this.normalizePathName || (normalize && this.normalizePathName !== false)) { - pathName = ModelBuilder.normalizePathName(pathName, className); - } if (!className) { throw new Error('Class name required'); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 0e1244eb..14f45c16 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -555,21 +555,6 @@ describe('DataSource define model', function () { 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'); - }); }); From 93aea7eb66095073160480014f6ebe164fb9e29d Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Mon, 4 Aug 2014 19:45:47 +0200 Subject: [PATCH 4/4] Changed options.path to option.http.path --- lib/model-builder.js | 3 ++- test/loopback-dl.test.js | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/model-builder.js b/lib/model-builder.js index 3aca2861..aa052dea 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -111,7 +111,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett var pluralName = (settings && settings.plural) || inflection.pluralize(className); - var pathName = (settings && settings.path) || pluralName; + var httpOptions = (settings && settings.http) || {}; + var pathName = httpOptions.path || pluralName; if (!className) { throw new Error('Class name required'); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 14f45c16..8a63a249 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -552,7 +552,9 @@ describe('DataSource define model', function () { it('should allow an explicit remoting path', function () { var ds = new DataSource('memory'); - var User = ds.define('User', {name: String, bio: String}, { path: 'accounts' }); + var User = ds.define('User', {name: String, bio: String}, { + http: { path: 'accounts' } + }); User.http.path.should.equal('/accounts'); });