Emit dataAccessConfigured events during attach
See discussions at https://github.com/strongloop/loopback/issues/410
This commit is contained in:
parent
1247c449b2
commit
b9d9ab0fb3
|
@ -393,7 +393,7 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
|
||||||
// Create a function for the closure in the loop
|
// Create a function for the closure in the loop
|
||||||
var createListener = function (name, relation, targetModel, throughModel) {
|
var createListener = function (name, relation, targetModel, throughModel) {
|
||||||
if (!isModelDataSourceAttached(targetModel)) {
|
if (!isModelDataSourceAttached(targetModel)) {
|
||||||
targetModel.once('dataSourceAttached', function (model) {
|
targetModel.once('dataAccessConfigured', function (model) {
|
||||||
// Check if the through model doesn't exist or resolved
|
// Check if the through model doesn't exist or resolved
|
||||||
if (!throughModel || isModelDataSourceAttached(throughModel)) {
|
if (!throughModel || isModelDataSourceAttached(throughModel)) {
|
||||||
// The target model is resolved
|
// The target model is resolved
|
||||||
|
@ -410,7 +410,7 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
|
||||||
}
|
}
|
||||||
if (throughModel && !isModelDataSourceAttached(throughModel)) {
|
if (throughModel && !isModelDataSourceAttached(throughModel)) {
|
||||||
// Set up a listener to the through model
|
// Set up a listener to the through model
|
||||||
throughModel.once('dataSourceAttached', function (model) {
|
throughModel.once('dataAccessConfigured', function (model) {
|
||||||
if (isModelDataSourceAttached(targetModel)) {
|
if (isModelDataSourceAttached(targetModel)) {
|
||||||
// The target model is resolved
|
// The target model is resolved
|
||||||
var params = traverse(relations).clone();
|
var params = traverse(relations).clone();
|
||||||
|
@ -479,9 +479,15 @@ DataSource.prototype.setupDataAccess = function (modelClass, settings) {
|
||||||
// add data access objects
|
// add data access objects
|
||||||
this.mixin(modelClass);
|
this.mixin(modelClass);
|
||||||
|
|
||||||
|
// define relations from LDL (options.relations)
|
||||||
var relations = settings.relationships || settings.relations;
|
var relations = settings.relationships || settings.relations;
|
||||||
this.defineRelations(modelClass, relations);
|
this.defineRelations(modelClass, relations);
|
||||||
|
|
||||||
|
// Emit the dataAccessConfigured event to indicate all the methods for data
|
||||||
|
// access have been mixed into the model class
|
||||||
|
modelClass.emit('dataAccessConfigured', modelClass);
|
||||||
|
|
||||||
|
// define scopes from LDL (options.relations)
|
||||||
var scopes = settings.scopes || {};
|
var scopes = settings.scopes || {};
|
||||||
this.defineScopes(modelClass, scopes);
|
this.defineScopes(modelClass, scopes);
|
||||||
|
|
||||||
|
|
|
@ -350,6 +350,34 @@ describe('DataSource define model', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should emit events during attach', function() {
|
||||||
|
var ds = new DataSource('memory');
|
||||||
|
var modelBuilder = new ModelBuilder();
|
||||||
|
|
||||||
|
var User = modelBuilder.define('User', {
|
||||||
|
name: String
|
||||||
|
});
|
||||||
|
|
||||||
|
var seq = 0;
|
||||||
|
var dataAccessConfigured = -1;
|
||||||
|
var dataSourceAttached = -1;
|
||||||
|
|
||||||
|
User.on('dataAccessConfigured', function (model) {
|
||||||
|
dataAccessConfigured = seq++;
|
||||||
|
assert(User.create);
|
||||||
|
assert(User.hasMany);
|
||||||
|
});
|
||||||
|
|
||||||
|
User.on('dataSourceAttached', function (model) {
|
||||||
|
assert(User.dataSource instanceof DataSource);
|
||||||
|
dataSourceAttached = seq++;
|
||||||
|
});
|
||||||
|
|
||||||
|
ds.attach(User);
|
||||||
|
assert.equal(dataAccessConfigured, 0);
|
||||||
|
assert.equal(dataSourceAttached, 1);
|
||||||
|
});
|
||||||
|
|
||||||
it('should not take unknown properties in strict mode', function (done) {
|
it('should not take unknown properties in strict mode', function (done) {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue