Set up the base model based on the connector types

This commit is contained in:
Raymond Feng 2014-07-20 11:52:12 -07:00
parent 4f7a9869e0
commit 643293cc25
2 changed files with 41 additions and 0 deletions

View File

@ -239,6 +239,19 @@ registry.createDataSource = function (name, options) {
var self = this; var self = this;
var ds = new DataSource(name, options, self.modelBuilder); var ds = new DataSource(name, options, self.modelBuilder);
ds.createModel = function (name, properties, settings) { 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); var ModelCtor = self.createModel(name, properties, settings);
ModelCtor.attachTo(ds); ModelCtor.attachTo(ds);
return ModelCtor; return ModelCtor;

View File

@ -33,6 +33,34 @@ describe('DataSource', function() {
assert.isFunc(Color.prototype, 'updateAttributes'); assert.isFunc(Color.prototype, 'updateAttributes');
assert.isFunc(Color.prototype, 'reload'); 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.equal(Color.super_, Base);
});
it("should use loopback.PersistedModel as the base for DBs", function() {
var Color = memory.createModel('color', {name: String});
assert.equal(Color.super_, 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.equal(Color.super_, loopback.Model);
});
}); });
describe.skip('PersistedModel Methods', function() { describe.skip('PersistedModel Methods', function() {