6

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.

2
  • Could you try to rephrase what's you're trying to do please? Because Backbone.Events is not really supposed to be used as an object. It only defines some method to extend all the other objects (Router, View, Collection, Model).
    – Loamhoof
    Apr 5, 2013 at 20:55
  • I'm sorry, think I didn't use the right wording. appEvents is working properly. I can do appEvents.trigger('event') just fine. What I'm trying to do is to somehow see in my console those events when they are triggered. So say I have an event called "openModal" being triggered when I click on a link, I'd like to have a console.log with the name "openModal" when I click that link that somehow works automatically when appEvents.trigger is called. Apr 5, 2013 at 21:02

4 Answers 4

39

You can just use Backbone's special all event:

appEvents.on("all", function(eventName){
    console.log(eventName + ' was triggered!');
});
4
  • See his comment, it's not listening, it's triggering.
    – Loamhoof
    Apr 5, 2013 at 21:08
  • This! Thank you! I knew it should be simple enough. Apr 5, 2013 at 21:10
  • 5
    Enrique, please mark this answer as correct, based on your last comment.
    – djmitche
    Jul 3, 2014 at 17:21
  • 5
    and @EnriqueRamírezVélez was never heard from again
    – dansch
    May 18, 2015 at 18:00
1
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

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).

1
  • Sorry I confused you (yet again). I used trigger as an example, but I was looking to see any changes on appEvents. @mVChr got it. THANK YOU! Apr 5, 2013 at 21:12
0

"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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.