Trunk events: logging all events when they are triggered

I create an event object using a bit of base and underline as follows:

var appEvents = _.extend({}, Backbone.Events);

Then I try to create a function that will trigger console.log everything and any event raised by this object, no matter where, how or what listeners it has, but I don’t know how to do it. I'm still experimenting with Backbone.

I think using the listenTo method is the way to go ... but then again, I don't know how to implement this.

+5
source share
3 answers

You can simply use a special event allfor the baseline:

appEvents.on("all", function(eventName){
    console.log(eventName + ' was triggered!');
});
+30
source

Cancel the start function then.

var trigger = appEvents.trigger;
appEvents.trigger = function(name) {
  console.log('Event', name, 'triggered.');
  trigger.apply(this, arguments);
};

( , ).

0
var object = {};

_.extend(object, Backbone.Events);

object.on("alert:param1", function(msg) {
  alert(" " + msg);
});


object.on("alert:param2", function(msg) {
  alert(" " + msg);
});



object.on("all", function(eventName) {
  console.log(eventName);
});
object.trigger("alert:param2", "");
object.trigger("alert:param1", "");
0

All Articles