I really want to go through every view programmatically and trigger all of their click events, is there a way to do this?
vent
to access view functionality(NOTE The vent
messaging system has been deprecated in Marionette 3.0, but I'll show you the general functionality here and then refer you to the docs to use Marionette.Wreqr, which will replace vent
. Wreqr is another Marionette hard dependency. Once you set up Wreqr (which is close to trivial, as the docs will show you) the rest of this explanation is practically identical.)
The basic usage as per the docs is:
var MyApp = new Backbone.Marionette.Application();
// Alert the user on the 'minutePassed' event
MyApp.vent.on("minutePassed", function(someData){
alert("Received", someData);
});
// This will emit an event with the value of window.someData every minute
window.setInterval(function() {
MyApp.vent.trigger("minutePassed", window.someData);
}, 1000 * 60);
And as you can see above you simply set a hanlder on the MyApp.vent
object, and when you trigger an event on MyApp.vent
the handler will be invoked. Note, that the parameters you trigger MyApp.vent
with will be passed to the handler function.
MyApp.vent
in your viewTo trigger a click on each view we set up the appropriate handler on your view:
var MyView = Marionette.ItemView.extend({
initialize: function () {
this.listenTo(MyApp.vent, "child:view:event", function () { this.$el.click() });
}
});
So in any other view, say the parent collection view, you can now trigger this event and the handlers in each child view will be invoked.
var MyCollectionView = Marionette.CollectionView.extend({
triggerChildren: function () {
MyApp.vent.trigger("child:view:event");
}
});
The full documentation for Backbone.Wreqr is here. But I'll show you how to set up Wreqr such that the above procedure doesn't have to be modified. Since Wreqr is loaded on the Backbone object by Marionette, all you have to do is set
MyApp.vent = new Backbone.Wreqr.EventAggregator();
and you're done.