我刚开始使用KnockoutJS编写打字稿,我必须承认我不知道我认为在这里非常有用的设计模式。因为,我需要一个模式。我想从父类中执行该功能,但我不知道实现这一目标的最佳方法是什么,我宁愿将整个对象传递给孩子们还是让一些订阅者/听众在这里工作?我真的不知道,正在寻求帮助。谢谢。以下缩短的代码:
module Desktop {
export class Note {
public message: any;
private duration: KnockoutObservable<string>;
private timer: number;
constructor(message: any) {
this.message = message;
this.duration = ko.observable(message.duration || 'short');
this.timer = setTimeout(() => !!THIS IS WHERE I WANT TO CALL killNote!!, 8000); //time enum
}
}
export class NotificationsVM {
public notes: KnockoutObservableArray<Note>;
constructor() {
this.notes = ko.observableArray([]);
//...
}
public addNote(msg) {
this.notes.push(new Note(msg));
}
public killNote(e) {
this.notes.remove(e);
}
}
}
<!-- ko with: notificationsVM -->
<aside data-bind="foreach: notes">
<div>
...
</div>
</aside>
<!-- /ko -->
简而言之:在我想要杀死它的每个音符指定不同的时间后(从音符数组中删除)。
答案 0 :(得分:2)
在为我要杀死的每个音符指定不同的时间之后
函数是javascript中的第一类变量。所以只需将其传递给:
module Desktop {
export class Note {
public message: any;
private duration: KnockoutObservable<string>;
private timer: number;
constructor(message: any, killNote) {
this.message = message;
this.duration = ko.observable(message.duration || 'short');
this.timer = setTimeout(() => killNote({}), 8000); //time enum
}
}
export class NotificationsVM {
public notes: KnockoutObservableArray<Note>;
constructor() {
this.notes = ko.observableArray([]);
//...
}
public addNote(msg) {
this.notes.push(new Note(msg, (e)=>killNote(e)));
}
public killNote(e) {
this.notes.remove(e);
}
}
}