所以我将两个模型传递到骨干视图中。我从视图的initialize函数中使用this.options.model2获得第二个模型。
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]的结果不是对象。
我猜这是因为我不了解触发器关闭时应用程序的范围。
答案 0 :(得分:1)
this
设置为jQuery事件回调中原始选择器中的元素。
...
var that = this;
$( window ).on( 'scroll', function () {
// here this = window
that.stateModel.set("savedScrollY", this.pageYOffset);
});