diff --git a/lib/model-builder.js b/lib/model-builder.js index e18aac89..7063915f 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -104,6 +104,7 @@ ModelBuilder.prototype.getModelDefinition = function (name) { * @param {String} className Name of class * @param {Object} properties Hash of class properties in format `{property: Type, property2: Type2, ...}` or `{property: {type: Type}, property2: {type: Type2}, ...}` * @param {Object} settings Other configuration of class + * @param {Function} parent Parent model * @return newly created class * */ @@ -152,6 +153,12 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett } } + // Make sure base properties are inherited + // See https://github.com/strongloop/loopback-datasource-juggler/issues/293 + if ((parent && !settings.base) || (!parent && settings.base)) { + return ModelBaseClass.extend(className, properties, settings); + } + // Check if there is a unresolved model with the same name var ModelClass = this.models[className]; @@ -349,8 +356,9 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett // Ensure 'base' is not inherited. Note we don't have to delete 'super' // as that is removed from settings by modelBuilder.define and thus // it is never inherited - if (!originalSubclassSettings.base) - delete subclassSettings.base; + if (!originalSubclassSettings.base) { + subclassSettings.base = ModelClass; + } // Define the subclass var subClass = modelBuilder.define(className, subclassProperties, subclassSettings, ModelClass); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index c3a47e2c..41fee2e8 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -660,6 +660,18 @@ describe('Load models with base', function () { } }); + it('should inherit properties from base option', function () { + var ds = new ModelBuilder(); + + var User = ds.define('User', {name: String}); + + var Customer = ds.define('Customer', {vip: Boolean}, {base: 'User'}); + + Customer.definition.properties.should.have.property('name'); + Customer.definition.properties.name.should.have.property('type', String); + }); + + it('should set up base class via parent arg', function () { var ds = new ModelBuilder(); @@ -672,6 +684,9 @@ describe('Load models with base', function () { var Customer = ds.define('Customer', {vip: Boolean}, {}, User); + Customer.definition.properties.should.have.property('name'); + Customer.definition.properties.name.should.have.property('type', String); + assert(Customer.prototype instanceof User); assert(Customer.staticMethod === User.staticMethod); assert(Customer.prototype.instanceMethod === User.prototype.instanceMethod); @@ -1646,7 +1661,8 @@ describe('Load models from json', function () { model: 'Order' } }, - strict: false + strict: false, + base: User }); done();