Make sure base properties/settings are merged into the submodel

See https://github.com/strongloop/loopback-datasource-juggler/issues/293
This commit is contained in:
Raymond Feng 2015-02-07 11:15:28 -08:00
parent e9c966227d
commit cd2bd34619
2 changed files with 27 additions and 3 deletions

View File

@ -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);

View File

@ -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();