diff --git a/templates/app/server/api/user(auth)/user.events.js b/templates/app/server/api/user(auth)/user.events.js index f8ebebe90..4e3ec953a 100644 --- a/templates/app/server/api/user(auth)/user.events.js +++ b/templates/app/server/api/user(auth)/user.events.js @@ -4,8 +4,7 @@ 'use strict'; -import {EventEmitter} from 'events';<% if (filters.mongooseModels) { %> -import User from './user.model';<% } if (filters.sequelizeModels) { %> +import {EventEmitter} from 'events';<% if (filters.sequelizeModels) { %> import {User} from '../../sqldb';<% } %> var UserEvents = new EventEmitter(); @@ -24,10 +23,12 @@ var events = { };<% } %> // Register the event emitter to the model events -for(var e in events) { - let event = events[e];<% if (filters.mongooseModels) { %> - User.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %> - User.hook(e, emitEvent(event));<% } %> +function registerEvents(User) { + for(var e in events) { + let event = events[e];<% if (filters.mongooseModels) { %> + User.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %> + User.hook(e, emitEvent(event));<% } %> + } } function emitEvent(event) { @@ -37,5 +38,7 @@ function emitEvent(event) { done(null);<% } %> } } - +<% if (filters.sequelizeModels) { %> +registerEvents(User);<% } if (filters.mongooseModels) { %> +export {registerEvents};<% } %> export default UserEvents; diff --git a/templates/app/server/api/user(auth)/user.model(mongooseModels).js b/templates/app/server/api/user(auth)/user.model(mongooseModels).js index f23208f12..b19e3ff36 100644 --- a/templates/app/server/api/user(auth)/user.model(mongooseModels).js +++ b/templates/app/server/api/user(auth)/user.model(mongooseModels).js @@ -2,7 +2,8 @@ /*eslint no-invalid-this:0*/ import crypto from 'crypto'; mongoose.Promise = require('bluebird'); -import mongoose, {Schema} from 'mongoose';<% if(filters.oauth) { %> +import mongoose, {Schema} from 'mongoose'; +import {registerEvents} from './user.events';<% if(filters.oauth) { %> const authTypes = ['github', 'twitter', 'facebook', 'google'];<% } %> @@ -257,4 +258,5 @@ UserSchema.methods = { } }; +registerEvents(UserSchema); export default mongoose.model('User', UserSchema); diff --git a/templates/endpoint/basename.events(models).js b/templates/endpoint/basename.events(models).js index 374c8108f..6040e60dd 100644 --- a/templates/endpoint/basename.events(models).js +++ b/templates/endpoint/basename.events(models).js @@ -4,8 +4,7 @@ 'use strict'; -import {EventEmitter} from 'events';<% if(filters.mongooseModels) { %> -import <%= classedName %> from './<%= basename %>.model';<% } if(filters.sequelizeModels) { %> +import {EventEmitter} from 'events';<% if(filters.sequelizeModels) { %> var <%= classedName %> = require('<%= relativeRequire(config.get('registerModelsFile')) %>').<%= classedName %>;<% } %> var <%= classedName %>Events = new EventEmitter(); @@ -27,10 +26,12 @@ var events = { <%_ } -%> // Register the event emitter to the model events -for(var e in events) { - let event = events[e];<% if(filters.mongooseModels) { %> - <%= classedName %>.schema.post(e, emitEvent(event));<% } if(filters.sequelizeModels) { %> - <%= classedName %>.hook(e, emitEvent(event));<% } %> +function registerEvents(<%= classedName %>) { + for(var e in events) { + let event = events[e];<% if(filters.mongooseModels) { %> + <%= classedName %>.post(e, emitEvent(event));<% } if(filters.sequelizeModels) { %> + <%= classedName %>.hook(e, emitEvent(event));<% } %> + } } function emitEvent(event) { @@ -40,5 +41,7 @@ function emitEvent(event) { done(null);<% } %> }; } - +<% if (filters.sequelizeModels) { %> +registerEvents(<%= classedName %>);<% } if (filters.mongooseModels) { %> +export {registerEvents};<% } %> export default <%= classedName %>Events; diff --git a/templates/endpoint/basename.model(mongooseModels).js b/templates/endpoint/basename.model(mongooseModels).js index 58391e89a..201a56502 100644 --- a/templates/endpoint/basename.model(mongooseModels).js +++ b/templates/endpoint/basename.model(mongooseModels).js @@ -1,6 +1,7 @@ 'use strict'; import mongoose from 'mongoose'; +import {registerEvents} from './<%= basename %>.events'; var <%= classedName %>Schema = new mongoose.Schema({ name: String, @@ -8,4 +9,5 @@ var <%= classedName %>Schema = new mongoose.Schema({ active: Boolean }); +registerEvents(<%= classedName %>Schema); export default mongoose.model('<%= classedName %>', <%= classedName %>Schema);