Skip to content

Instantly share code, notes, and snippets.

@ptomasroos
Forked from jstrimpel/gist:7aa111403f555986b7e9
Last active August 29, 2015 14:06
Show Gist options
  • Save ptomasroos/b2abfa9d591cc99e6769 to your computer and use it in GitHub Desktop.
Save ptomasroos/b2abfa9d591cc99e6769 to your computer and use it in GitHub Desktop.
// 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