Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Events: logging all events when they are triggered

Tags:

backbone.js

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.

like image 655
Enrique Ramírez Vélez Avatar asked Apr 05 '13 20:04

Enrique Ramírez Vélez


People also ask

How do you trigger an event in the backbone?

Backbone. js trigger Event is used to invoke or callback the function for the given event or a space-delimited list of events. The subsequent arguments will be passed along to the event callbacks in order to trigger it.

What is Backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).

How Backbone js works?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.


2 Answers

You can just use Backbone's special all event:

appEvents.on("all", function(eventName){
    console.log(eventName + ' was triggered!');
});
like image 62
mVChr Avatar answered Oct 20 '22 18:10

mVChr


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", "событие");
like image 45
zloctb Avatar answered Oct 20 '22 18:10

zloctb