Reputation: 5335
I have the following typescript file in Nativescript:
import { Observable } from 'tns-core-modules/data/observable';
import { isIOS } from "tns-core-modules/platform";
import { Color } from "tns-core-modules/color";
import { request, getFile, getImage, getJSON, getString } from "tns-core-modules/http";
export class HomeViewModel extends Observable {
items: {
name: string,
desc: string,
price: string,
imageSrc: string,
}[];
getData = getJSON("http://localhost:3000/items").then((r: any) => {
this.getData = r; // assign it from the response when successful
console.log("blarg!!!")
}, (e) => {
});
onItemLoading(args) {
// hack to get around issue with RadListView ios background colors: https://github.com/telerik/nativescript-ui-feedback/issues/196
if (isIOS) {
var newcolor = new Color("#e6e6e6");
args.ios.backgroundView.backgroundColor = newcolor.ios;
}
}
constructor() {
super();
}
}
The issue I am seeing is that getData is never being called/ran. How do I call/run the getData function?
Upvotes: 0
Views: 78
Reputation: 14030
This is really not type-safe and you probably don't mean to do this.
getData = getJSON("http://localhost:3000/items").then((r: any) => {
this.getData = r; // assign it from the response when successful
console.log("blarg!!!")
}, (e) => {
});
If you expose the type from the initial assignment, you've got:
getData: Promise<void> = ...
If you expose the type from the inner assignment, you've got:
this.getData = r as any;
It is very likely that neither of these is actually a function. Instead, what you probably wanted to do was something more along the lines of:
data: any;
constructor() {
super();
getJSON("http://localhost:3000/items").then((r: any) => {
this.data = r; // assign it from the response when successful
console.log("blarg!!!")
}, (e) => {
});
}
Upvotes: 1
Reputation: 1535
You need to call it higher up the stack. Or, If you want getData
to run when the object is created, call getData()
in your constructor.
Upvotes: 0