Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
revisiting event aggregator
MyApp = {};
MyApp.vent = _.extend({}, Backbone.Events);
MyApp.vent.on("some:event", function(){
//do stuff common to ALL events
//usually you won't have logic here
});
var ObservingModel = Backbone.Model.extend({
initialize : function () {
MyApp.vent.bind('some:event', this.reactToObservation)
},
reactToObservation : function() { console.log('event came through loud and clear'); }
});
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
Something went wrong with that request. Please try again.