Fix minor autoWiring bugs

This commit is contained in:
Ritchie Martori 2013-11-20 14:18:54 -08:00
parent 3753c15b71
commit aaa8423257
2 changed files with 18 additions and 7 deletions

View File

@ -221,8 +221,16 @@ app.boot = function(options) {
app.model(key, obj);
});
// attach models to dataSources by type
loopback.autoAttach();
// try to attach models to dataSources by type
try {
require('./loopback').autoAttach();
} catch(e) {
if(e.name === 'AssertionError') {
console.warn(e);
} else {
throw e;
}
}
// require directories
var requiredModels = requireDir(path.join(appRootDir, 'models'));

View File

@ -97,8 +97,8 @@ loopback.createDataSource = function (name, options) {
return ModelCtor;
};
if(options && options.defaultForType) {
loopback.setDefaultDataSourceForType(options.defaultForType, ds);
if(ds.settings && ds.settings.defaultForType) {
loopback.setDefaultDataSourceForType(ds.settings.defaultForType, ds);
}
return ds;
@ -116,7 +116,9 @@ loopback.createModel = function (name, properties, options) {
var model = loopback.Model.extend(name, properties, options);
// try to attach
try {
loopback.autoAttachModel(model);
} catch(e) {}
return model;
}
@ -228,8 +230,9 @@ loopback.autoAttachModel = function(ModelCtor) {
if(ModelCtor.autoAttach) {
var ds = loopback.getDefaultDataSourceForType(ModelCtor.autoAttach);
assert(ds instanceof DataSource, 'cannot autoAttach model "' + ModelCtor.modelName
+ '". No dataSource found of type ' + ModelCtor.attachTo);
assert(ds instanceof DataSource, 'cannot autoAttach model "'
+ ModelCtor.modelName
+ '". No dataSource found of type ' + ModelCtor.autoAttach);
ModelCtor.attachTo(ds);
}