I am looking for a better way to implement PubSub type functionality using Backbone. I am currently seeking this, but I am looking for a better way to do this.
Routing Code Example
var AppRouter = Backbone.Router.extend({ customEvents: _.extend({}, Backbone.Events), routes: { "login": "login" }, //Injecting custom event login: function(){ this.before(function () { app.showView('#Content', new LoginView({customEvents:this.customEvents}), "login", true); }); }, //Injecting custom event otherView: function(){ this.before(function () { app.showView('#Header', new OtherView({customEvents:this.customEvents}), "login", true); }); }, ..... more code });
View code example. Note that I am using bind to listen for customEvent. This works great, but is looking for an alternative method
LoginView = Backbone.View.extend({ initialize: function(options){ this.customEvents = options.customEvents; this.customEvents.bind('app:loggedin', this.loggedIn); }, loggedIn: function(){ console.log("LOG CHANGED"); }, ...more code
I would rather save my events with the rest of the events that I use in my view. Not sure how to achieve this. Should I extend the View class?
What I would like to do in my ideas
LoginView = Backbone.View.extend({ events: { "app:loggin" : "loggedIn" }, loggedIn: function(){ console.log("LOG CHANGED"); }, ...more code
source share