必须将回调声明为平面函数,而不是函数的结果。看到这个小道消息了吗,可以在Typescript草稿行上运行
by clicking here
. 我确实提出了一些意见,比如“/”这是一个重要的变化“在这里适用。
class Item {
Published: boolean
}
class Observable {
subscribe(callback: (data: Array<Item>) => any) {
callback(new Array<Item>());
}
}
class Test {
apiUrl: string;
http = {
patch: (url, item, httpOptions) => {
return new Observable();
}
};
item$ = new Item();
items$: Item[];
id: 'someid';
edit = {
editItem: (item: any, id: string, callback: (data: Item[]) => void) => {
// THIS IS THE IMPORTANT CHANGE
return this.editItem(item, id, callback);
}
}
data = {
getItems: () => {
return new Observable();
}
};
editItem(item, id, getItems) {
const httpOptions: any = {};
const url = this.apiUrl + '/tables/Items/' + id
this.http.patch(url, item, httpOptions).subscribe((data) => this.getItems(data));
}
getItems(itemList: Item[]): void {
this.data.getItems()
.subscribe(data => { this.items$ = data; alert("Hey! I got called"); });
}
SaveItem(flag: boolean) {
this.item$.Published = flag;
// THIS IS THE IMPORTANT CHANGE
this.edit.editItem(this.item$, this.id, this.getItems);
}
}
let test = new Test();
test.SaveItem(true);