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:
parent
e9c966227d
commit
cd2bd34619
|
@ -104,6 +104,7 @@ ModelBuilder.prototype.getModelDefinition = function (name) {
|
||||||
* @param {String} className Name of class
|
* @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} 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 {Object} settings Other configuration of class
|
||||||
|
* @param {Function} parent Parent model
|
||||||
* @return newly created class
|
* @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
|
// Check if there is a unresolved model with the same name
|
||||||
var ModelClass = this.models[className];
|
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'
|
// Ensure 'base' is not inherited. Note we don't have to delete 'super'
|
||||||
// as that is removed from settings by modelBuilder.define and thus
|
// as that is removed from settings by modelBuilder.define and thus
|
||||||
// it is never inherited
|
// it is never inherited
|
||||||
if (!originalSubclassSettings.base)
|
if (!originalSubclassSettings.base) {
|
||||||
delete subclassSettings.base;
|
subclassSettings.base = ModelClass;
|
||||||
|
}
|
||||||
|
|
||||||
// Define the subclass
|
// Define the subclass
|
||||||
var subClass = modelBuilder.define(className, subclassProperties, subclassSettings, ModelClass);
|
var subClass = modelBuilder.define(className, subclassProperties, subclassSettings, ModelClass);
|
||||||
|
|
|
@ -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 () {
|
it('should set up base class via parent arg', function () {
|
||||||
var ds = new ModelBuilder();
|
var ds = new ModelBuilder();
|
||||||
|
|
||||||
|
@ -672,6 +684,9 @@ describe('Load models with base', function () {
|
||||||
|
|
||||||
var Customer = ds.define('Customer', {vip: Boolean}, {}, User);
|
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.prototype instanceof User);
|
||||||
assert(Customer.staticMethod === User.staticMethod);
|
assert(Customer.staticMethod === User.staticMethod);
|
||||||
assert(Customer.prototype.instanceMethod === User.prototype.instanceMethod);
|
assert(Customer.prototype.instanceMethod === User.prototype.instanceMethod);
|
||||||
|
@ -1646,7 +1661,8 @@ describe('Load models from json', function () {
|
||||||
model: 'Order'
|
model: 'Order'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
strict: false
|
strict: false,
|
||||||
|
base: User
|
||||||
});
|
});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
Loading…
Reference in New Issue