Created
September 19, 2014 00:03
-
-
Save jstrimpel/7aa111403f555986b7e9 to your computer and use it in GitHub Desktop.
socket.io and lazo model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in a controller | |
// you can either update the collection directly or send | |
// a message in the LAZO.app dispatcher | |
// (you can use a controller dispatcher if you want to be more granular) | |
var socket = io(); | |
var self = this; | |
var notifications = this.ctx.collections.notifications; | |
// three different approaches to re-rendering a badge count | |
socket.on('notification', function (msg) { | |
// manipulate collection directly | |
notifications.add(msg); | |
// app level dispatcher | |
LAZO.app.trigger('notification', msg); | |
// your base class could have a simpler broadcast, emitter functionality | |
// (if you want to know how to accomplish this let me know) | |
self.trigger('notification', msg); | |
}); | |
// if notifications is driving a lazo collection view then the UI will be updated | |
// if you only care about updating a badge count then you can bind to the collection | |
// change event in a lazo base view | |
afterRender: function () { | |
var notifications = this.ctl.ctx.collections.notifications; | |
// listener is dependent on how you are triggered the message | |
// collection based change listener | |
self.listenTo(notifications, 'all', function () { | |
self.notificationCount = notifications.length; | |
self.render(); | |
}); | |
// app level dispatcher | |
LAZO.app.on('notification', function () { | |
self.notificationCount = notifications.length; | |
self.render(); | |
}); | |
this.ctl.on('notification', function () { | |
self.notificationCount = notifications.length; | |
self.render(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment