- 
      
 - 
        
Save mxriverlynn/2146930 to your computer and use it in GitHub Desktop.  
    revisiting event aggregator
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | MyApp = {}; | |
| MyApp.vent = _.extend({}, Backbone.Events); | |
| MyApp.vent.on("some:event", function(){ | |
| alert("some event was fired!"); | |
| }); | |
| MyApp.vent.trigger("some:event"); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | MyApp = new Backbone.Marionette.Application(); | |
| MyApp.vent.on("some:event", function(){ | |
| alert("some event was fired!"); | |
| }); | |
| MyApp.vent.trigger("some:event"); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | MyApp.vent.on("myChannel", "myEvent", function(){ | |
| // do stuff here | |
| }); | |
| MyApp.vent.trigger("myChannel", "myEvent"); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | MyApp.myChannel = _.extend({}, Backbone.Events); | |
| MyApp.anotherChannel = _.extend({}, Backbone.Events); | |
| MyApp.myChannel.on("someEvent", function(){ | |
| // ... | |
| }); | |
| MyApp.myChannel.on("anotherEvent", function(){ | |
| // ... | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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