diff --git a/app/templates/server/api/user(auth)/user.events.js b/app/templates/server/api/user(auth)/user.events.js new file mode 100644 index 000000000..102fd5d55 --- /dev/null +++ b/app/templates/server/api/user(auth)/user.events.js @@ -0,0 +1,41 @@ +/** + * User model events + */ + +'use strict'; + +var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %> +var User = require('./user.model');<% } if (filters.sequelizeModels) { %> +var User = require('../../sqldb').User;<% } %> +var UserEvents = new EventEmitter(); + +// Set max event listeners (0 == unlimited) +UserEvents.setMaxListeners(0); + +// Model events<% if (filters.mongooseModels) { %> +var events = { + 'save': 'save', + 'remove': 'remove' +};<% } if (filters.sequelizeModels) { %> +var events = { + 'afterCreate': 'save', + 'afterUpdate': 'save', + 'afterDestroy': 'remove' +};<% } %> + +// Register the event emitter to the model events +for (var e in events) { + var event = events[e];<% if (filters.mongooseModels) { %> + User.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %> + User.hook(e, emitEvent(event));<% } %> +} + +function emitEvent(event) { + return function(doc<% if (filters.sequelizeModels) { %>, options, done<% } %>) { + UserEvents.emit(event + ':' + doc._id, doc); + UserEvents.emit(event, doc);<% if (filters.sequelizeModels) { %> + done(null);<% } %> + } +} + +module.exports = UserEvents; diff --git a/endpoint/templates/name.events(models).js b/endpoint/templates/name.events(models).js new file mode 100644 index 000000000..8bda5cf9a --- /dev/null +++ b/endpoint/templates/name.events(models).js @@ -0,0 +1,41 @@ +/** + * <%= classedName %> model events + */ + +'use strict'; + +var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %> +var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %> +var <%= classedName %> = require('../../sqldb').<%= classedName %>;<% } %> +var <%= classedName %>Events = new EventEmitter(); + +// Set max event listeners (0 == unlimited) +<%= classedName %>Events.setMaxListeners(0); + +// Model events<% if (filters.mongooseModels) { %> +var events = { + 'save': 'save', + 'remove': 'remove' +};<% } if (filters.sequelizeModels) { %> +var events = { + 'afterCreate': 'save', + 'afterUpdate': 'save', + 'afterDestroy': 'remove' +};<% } %> + +// Register the event emitter to the model events +for (var e in events) { + var event = events[e];<% if (filters.mongooseModels) { %> + <%= classedName %>.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %> + <%= classedName %>.hook(e, emitEvent(event));<% } %> +} + +function emitEvent(event) { + return function(doc<% if (filters.sequelizeModels) { %>, options, done<% } %>) { + <%= classedName %>Events.emit(event + ':' + doc._id, doc); + <%= classedName %>Events.emit(event, doc);<% if (filters.sequelizeModels) { %> + done(null);<% } %> + } +} + +module.exports = <%= classedName %>Events; diff --git a/endpoint/templates/name.socket(socketio).js b/endpoint/templates/name.socket(socketio).js index e55dfcac0..24d744d13 100644 --- a/endpoint/templates/name.socket(socketio).js +++ b/endpoint/templates/name.socket(socketio).js @@ -3,31 +3,32 @@ */ 'use strict'; -<% if (filters.mongoose) { %> -var <%= classedName %> = require('./<%= name %>.model');<% } %><% if (filters.sequelize) { %> -var <%= classedName %> = require('../../sqldb').<%= classedName %>;<% } %> - -exports.register = function(socket) {<% if (filters.sequelize) { %> - <%= classedName %>.hook('afterCreate', function(doc, fields, fn) { - onSave(socket, doc); - fn(null); - });<% } %> - <% if (filters.mongoose) { %><%= classedName %>.schema.post('save', function(doc) {<% } - if (filters.sequelize) { %><%= classedName %>.hook('afterUpdate', function(doc, fields, fn) {<% } %> - onSave(socket, doc);<% if (filters.sequelize) { %> - fn(null);<% } %> - }); - <% if (filters.mongoose) { %><%= classedName %>.schema.post('remove', function(doc) {<% } - if (filters.sequelize) { %><%= classedName %>.hook('afterDestroy', function(doc, fields, fn) {<% } %> - onRemove(socket, doc);<% if (filters.sequelize) { %> - fn(null);<% } %> - }); + +var <%= classedName %>Events = require('./<%= name %>.events'); + +// Model events to emit +var events = ['save', 'remove']; + +exports.register = function(socket) { + // Bind model events to socket events + for (var i = 0, eventsLength = events.length; i < eventsLength; i++) { + var event = events[i]; + var listener = createListener('<%= name %>:' + event, socket); + + <%= classedName %>Events.on(event, listener); + socket.on('disconnect', removeListener(event, listener)); + } }; -function onSave(socket, doc, cb) { - socket.emit('<%= name %>:save', doc); + +function createListener(event, socket) { + return function(doc) { + socket.emit(event, doc); + }; } -function onRemove(socket, doc, cb) { - socket.emit('<%= name %>:remove', doc); +function removeListener(event, listener) { + return function() { + <%= classedName %>Events.removeListener(event, listener); + }; } diff --git a/test/test-file-creation.js b/test/test-file-creation.js index bf5f00301..09e92264b 100644 --- a/test/test-file-creation.js +++ b/test/test-file-creation.js @@ -235,6 +235,7 @@ describe('angular-fullstack generator', function () { if (models) { files = files.concat([ 'server/api/thing/thing.model.js', + 'server/api/thing/thing.events.js', 'server/config/seed.js' ]); } @@ -270,6 +271,7 @@ describe('angular-fullstack generator', function () { 'server/api/user/user.integration.js', 'server/api/user/user.model.js', 'server/api/user/user.model.spec.js', + 'server/api/user/user.events.js', 'server/auth/index.js', 'server/auth/auth.service.js', 'server/auth/local/index.js',