Reputation: 1397
I have a backbone marionette view in which i have defined many events. I want to store a log for any event which is occuring inside my itemview. I dont want to write function calling for my audit data on every data, instead i had like to override backbone marionette events method so that it is applied on all my ItemViews.
I tried using:
var originalTrigger = Backbone.Events.trigger;
Backbone.Events.trigger = function(){
console.log("Event Triggered:");
console.log(arguments.join(", "));
originalTrigger.apply(this, arguments);
}
But it doesnt do anything for me. Please help
Thank you in advance
Upvotes: 3
Views: 825
Reputation: 434685
If you check the Backbone source you'll see things like this:
_.extend(Model.prototype, Events, {
where Model
and Events
are local aliases for Backbone.Model
and Backbone.Events
respectively. That means that the methods from Backbone.Events
will have been copied to the model, collection, and view prototypes before you have a chance to wrap Backbone.Events.trigger
with your auditing.
You'll want to wrap all four trigger
s after Backbone is loaded but before anything else (including Marionette) is loaded. Something like this:
(function() {
var trigger = Backbone.Events.trigger;
var wrapper = function() {
console.log("Event Triggered:");
console.log(arguments.join(", "));
trigger.apply(this, arguments);
};
Backbone.Model.prototype.trigger = wrapper;
Backbone.Collection.prototype.trigger = wrapper;
Backbone.View.prototype.trigger = wrapper;
})();
after <script src="backbone.js">
but before <script src="backbone.marionette.js">
should do the trick.
Upvotes: 3