Backbone Events: logging all events when they are triggered

I'm creating an events object using a bit of backbone and underscore as follows:

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

I'm then trying to create a function that will console.log all and any event triggered to this object, regardless of where, how or what listeners it has, but am kinda unsure how I'd do that. I'm still experimenting with Backbone.

I think using the listenTo method is the way to go... but again, not sure how I'd implement that.

Upvotes: 6

Views: 10078

Answers (4)

mr.Vash
mr.Vash

Reputation: 31

"add" (model, collection, options) — when a model is added to a collection.

"remove" (model, collection, options) — when a model is removed from a collection.

"update" (collection, options) — single event triggered after any number of models have been added, removed or changed in a collection.

"reset" (collection, options) — when the collection's entire contents have been reset.

"sort" (collection, options) — when the collection has been re-sorted.

"change" (model, options) — when a model's attributes have changed.

"changeId" (model, previousId, options) — when the model's id has been updated.

"change:[attribute]" (model, value, options) — when a specific attribute has been updated.

"destroy" (model, collection, options) — when a model is destroyed.

"request" (model_or_collection, xhr, options) — when a model or collection has started a request to the server.

"sync" (model_or_collection, response, options) — when a model or collection has been successfully synced with the server.

"error" (model_or_collection, xhr, options) — when a model's or collection's request to the server has failed.

"invalid" (model, error, options) — when a model's validation fails on the client.

"route:[name]" (params) — Fired by the router when a specific route is matched.

"route" (route, params) — Fired by the router when any route has been matched.

"route" (router, route, params) — Fired by history when any route has been matched.

"all" — this special event fires for any triggered event, passing the event name as the first argument followed by all trigger arguments.

Upvotes: 0

zloctb
zloctb

Reputation: 11184

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", "событие");

Upvotes: 1

Loamhoof
Loamhoof

Reputation: 8293

Override the trigger function then.

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

But I still find the whole story weird (again, that's only my opinion).

Upvotes: 0

mVChr
mVChr

Reputation: 50185

You can just use Backbone's special all event:

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

Upvotes: 39

Related Questions