javascript - typescipt say to all public variables "undefined" -
i got following code
module absence { export var instance: absenceviewmodel; export class viewmodel { public items: knockoutobservablearray<absenceitem>; public reloaddata() { this.items.removeall(); } } }
in page create instance using this
absence.instance = new absence.absenceviewmodel();
now when call
absence.instance.reloaddata();
it in browser console
cannot read property 'removeall' of undefined
but imported knockout libary , other needed stuff. present.
when use internal instance
absence.instance.items.removeall();
what wrong?
edit:
i wrote dcklaration of viewmodel , declaration of instance in separate js file. these files loaden in page. after page ready instantiate instance of viewmodel
hand in debug console. in call method reloaddata
. can sure classes available @ moment when call methods.
i'm not yet familiar get
mechanism in typescript, while able solve issue it, i'm adding answer, believe general solution tackling problem.
the following error:
cannot read property 'removeall' of undefined
is caused fact not initialize items
array. have declared property of viewmodel
class. adding constructor , initlaizing properties in correct manner avoids these kind of issues. hence, update code to:
module absence { export var instance: absenceviewmodel; export class viewmodel { public items: knockoutobservablearray<absenceitem>; constructor(){ this.items = ko.observablearray(); } public reloaddata() { this.items.removeall(); } } }
Comments
Post a Comment