See Knockout Binding Context. Because click
is on the prototype and is invoked without any explicit this
context, invoking it loses which class instance it belonged to.
You can also fix this by using an arrow function in your ViewModel definition for any event handlers that will be invoked this way:
class LookupEditorVM {
lookups: KnockoutObservableArray<LookupVM>;
selected: KnockoutObservable<LookupVM>;
click = (item) => { // <--- Arrow function preserves 'this'
this.selected(item)
}
constructor(baseURL: string) {
this.lookups = ko.observableArray<LookupVM>([]);
$.getJSON(baseURL, (data) => {
ko.mapping.fromJS(data, {}, this.lookups);
});
this.selected = ko.observable(undefined);
}
}