0

I trigger an event from one view like this:

select: function () {
    // Shorthand for the application namespace
    var app = brickpile.app;
    // Trigger the selected event
    app.trigger('selected', this.model);
}

and binds to the same event in another view:

initialize: function () {
    // Shorthand for the application namespace
    var app = brickpile.app;
    // bind to the selected event
    app.bind('selected', this.selected);
},

in my function I get the current instance el property?

selected: function (model) {
    // find the input hidden located in this views el
    $(this.el)... // is undefined
},

what have I missed?

marcus
  • 9,616
  • 9
  • 58
  • 108

1 Answers1

1

I'll quote Backbone FAQ to answer your question

Binding "this"

Perhaps the single most common JavaScript "gotcha" is the fact that when you pass a function as a callback, its value for this is lost. With Backbone, when dealing with events and callbacks, you'll often find it useful to rely on _.bind and _.bindAll from Underscore.js.

When binding callbacks to Backbone events, you can choose to pass an optional third argument to specify the this that will be used when the callback is later invoked.

Try

app.bind('selected', this.selected, this);

or

_.bindAll(this, 'selected');
app.bind('selected', this.selected);
Community
  • 1
  • 1
nikoshr
  • 32,926
  • 33
  • 91
  • 105