Skip to content

Instantly share code, notes, and snippets.

@Doogie04
Forked from mxriverlynn/1.js
Created August 1, 2012 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Doogie04/3230913 to your computer and use it in GitHub Desktop.
Save Doogie04/3230913 to your computer and use it in GitHub Desktop.
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