typescript - `this` in callback points to wrong object -
i have viewmodel:
import app = require("durandal/app"); import appviewmodel = require("appviewmodel"); import dataservice = require("dataservice"); class home{ section = ko.observable<string>(); activescreen = ko.observable<string>("nativelanguage/selectnativelanguage"); constructor() { app.on('home:activateview').then(this.activateview); } activateview(view) { this.activescreen(view); } activate() { return this.getsection(); } getsection() { return dataservice.getsection('home_page').then((data) => { this.section(data.results[0]); }); } } export = home; this compiles without error.
however, when runs, , activateview called callback, this pointing app not home activescreen property undefined.
how resolve this?
since passing function else call in .then(this.activateview); need preserve context yourself, best if : .then((view)=>this.activateview(view));
more this : https://www.youtube.com/watch?v=tvocucbcupa
Comments
Post a Comment