0

したがって、2つのモデルをバックボーンビューに渡します。ビューの初期化関数内からthis.options.model2を使用して2番目のモデルを取得します。

App.MyView = Backbone.View.extend({
initialize: function() {
        this.stateModel = this.options.model2;

        // test to make sure the stateModel is being set correctly. This works.
        console.log("test: : " + this.stateModel.get("blah"));

        // Save scroll position in model2 on scroll
        $( window ).on( 'scroll', function () {
            this.stateModel.set("savedScrollY", this.pageYOffset);
        });
});

スクロールすると、次のエラーが発生します。

TypeError:式の結果'this.stateModel'[undefined]はオブジェクトではありません。

これは、トリガーがオフになったときにアプリがどのスコープにあるかを理解していない結果だと思います。

4

1 に答える 1

1

thisjQueryイベントコールバック内の元のセレクターの要素に設定されます。

...
var that = this;
$( window ).on( 'scroll', function () {
    // here this = window
    that.stateModel.set("savedScrollY", this.pageYOffset);
});
于 2012-10-03T22:04:52.163 に答える