Issue with child scoping of this in Typescript

被刻印的时光 ゝ 提交于 2019-12-05 19:25:16

Yes there is, and it's actually pretty easy. Just use the lambda expression on any method you want to get scoped to the scope of the higher function. In your case you need to rewrite your example as:

class ViewModel
{
    public SomeObservableArray = new ko.observableArray();
    public AddToTheObservableArray = (someJsonData: any) => Void;


    constructor()
    {
        this.PopulateObservableArray = (someJsonArrayData: any) => {
            this.SomeObservableArray.removeAll();
            someJsonArrayData.forEach((someJsonData) => {
                this.SomeObservableArray.push(new SomePojo(someJsonData));
            });
        };
    }
}

PS: it's considered a best practice not to manipulate an observable array in a for each, as the subscribers of that array will be notified on each change. Just push your data to a temporary array and then set this array as the value of your observable array (just my 2cts.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!