So I’m passing two models into a backbone view. I get the second model using this.options.model2 from within the initialize function of the view.
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);
});
});
When I scroll I’m getting the error:
TypeError: Result of expression ‘this.stateModel’ [undefined] is not an object.
I’m guessing this is a result of me not understanding what scope the app is in when the trigger goes off.
thisis set to the element(s) in the original selector inside jQuery event callbacks.