0

I am creating custom events for a list of events. I have these events in a file (event.js) like this:

define(function(require, exports, module) {
module.exports =  {

    'event1': ['test1','data1'],
    'event2': ['test2','data2']
  };
});

I am creating event trigger for each of these events:

createTriggers: function(Events){ //Events = require(event.js);
      _.defer(function(){
        _.each(Events,function(data,event){
           var events = function () { console.log("Data is "+this.data)};
           events = _.bind(events,data); 
           targ.events.bind(event,events); //targ is the Backbone event object
        });
      });
  }

But when I trigger any of the event:

  this.trigger("event1");

I get output as Data is undefined. Why the values '['test1','data1']' is not fetched?

1 Answer 1

3

Considering these 2 lines:

var events = function () { console.log("Data is "+this.data)};
events = _.bind(events,data);

Assuming that data at this point is an Array (['test1','data1']), your code does this:

console.log("Data is " + ['test1','data1'].data);

You probably want:

var events = function () { console.log("Data is "+this)};
events = _.bind(events,data);

or

var events = function () { console.log("Data is "+this.data)};
events = _.bind(events,{data: data});

see Underscore bind documentation for more examples.

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