Merge pull request #387 from strongloop/feature/fix-base-model

Set up the base model based on the connector types
This commit is contained in:
Raymond Feng 2014-07-22 10:58:33 -07:00
commit 13c876c154
2 changed files with 44 additions and 0 deletions

View File

@ -239,6 +239,19 @@ registry.createDataSource = function (name, options) {
var self = this;
var ds = new DataSource(name, options, self.modelBuilder);
ds.createModel = function (name, properties, settings) {
settings = settings || {};
var BaseModel = settings.base || settings.super;
if (!BaseModel) {
// Check the connector types
var connectorTypes = ds.connector && ds.connector.getTypes();
if (Array.isArray(connectorTypes) && connectorTypes.indexOf('db') !== -1) {
// Only set up the base model to PersistedModel if the connector is DB
BaseModel = self.PersistedModel;
} else {
BaseModel = self.Model;
}
settings.base = BaseModel;
}
var ModelCtor = self.createModel(name, properties, settings);
ModelCtor.attachTo(ds);
return ModelCtor;

View File

@ -33,6 +33,37 @@ describe('DataSource', function() {
assert.isFunc(Color.prototype, 'updateAttributes');
assert.isFunc(Color.prototype, 'reload');
});
it("should honor settings.base", function() {
var Base = memory.createModel('base');
var Color = memory.createModel('color', {name: String}, {base: Base});
assert(Color.prototype instanceof Base);
assert.equal(Color.base, Base);
});
it("should use loopback.PersistedModel as the base for DBs", function() {
var Color = memory.createModel('color', {name: String});
assert(Color.prototype instanceof loopback.PersistedModel);
assert.equal(Color.base, loopback.PersistedModel);
});
it("should use loopback.Model as the base for non DBs", function() {
// Mock up a non-DB connector
var Connector = function() {
};
Connector.prototype.getTypes = function() {
return ['rest'];
};
var ds = loopback.createDataSource({
connector: new Connector()
});
var Color = ds.createModel('color', {name: String});
assert(Color.prototype instanceof loopback.Model);
assert.equal(Color.base, loopback.Model);
});
});
describe.skip('PersistedModel Methods', function() {