Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created March 21, 2012 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mxriverlynn/2146930 to your computer and use it in GitHub Desktop.
Save mxriverlynn/2146930 to your computer and use it in GitHub Desktop.
revisiting event aggregator
MyApp = {};
MyApp.vent = _.extend({}, Backbone.Events);
MyApp.vent.on("some:event", function(){
alert("some event was fired!");
});
MyApp.vent.trigger("some:event");
MyApp = new Backbone.Marionette.Application();
MyApp.vent.on("some:event", function(){
alert("some event was fired!");
});
MyApp.vent.trigger("some:event");
MyApp = new Backbone.Marionette.Application();
MyApp.UserManagement = (function(Backbone){
var mgmt = {};
var mgmtEvents = new Backbone.Marionette.EventAggregator();
mgmt.UserListView = Backbone.View.extend({
events: {
"click .user": "userClicked"
},
userClicked: function(){
var user = // get the user that was clicked
mgmtEvents.trigger("user:selected", user);
},
// ...
});
mgmt.UserDetailView = Backbone.View.extend({
// ...
});
mgmtEvents.on("user:selected", function(user){
var view = new mgmt.UserDetailView({
model: user
});
view.render();
$("#detail").html(view.el);
});
return mgmt;
})(Backbone);
MyApp.vent.on("myChannel", "myEvent", function(){
// do stuff here
});
MyApp.vent.trigger("myChannel", "myEvent");
MyApp.myChannel = _.extend({}, Backbone.Events);
MyApp.anotherChannel = _.extend({}, Backbone.Events);
MyApp.myChannel.on("someEvent", function(){
// ...
});
MyApp.myChannel.on("anotherEvent", function(){
// ...
});
MyApp.vent.on("some:namespaced:event", function(){
// ...
});
MyApp.vent.trigger("some:namespaced:event");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment