0

次のような1つのビューからイベントをトリガーします。

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

別のビューで同じイベントにバインドします。

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

私の関数では、現在のインスタンスのelプロパティを取得しますか?

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

私は何を逃したのですか?

4

1 に答える 1

1

あなたの質問に答えるためにBackbone FAQを引用します

「これ」を縛る

おそらく最も一般的な JavaScript の「落とし穴」は、関数をコールバックとして渡すと、その値が失われるという事実です。Backbone でイベントとコールバックを処理する場合、Underscore.js の _.bind と _.bindAll を利用すると便利なことがよくあります。

コールバックをバックボーン イベントにバインドする場合、オプションの 3 番目の引数を渡して、コールバックが後で呼び出されるときに使用される this を指定できます。

試す

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

また

_.bindAll(this, 'selected');
app.bind('selected', this.selected);
于 2012-06-03T08:26:15.033 に答える