Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout Viewmodel in Typescript

How are we supposed to declare viewmodels in typescript?

As classes, modules or as var/functions?

In the definitelytyped examples they use var and function mostly https://github.com/borisyankov/DefinitelyTyped/blob/master/knockout/tests/knockout-tests.ts

EDIT: Thank you Basarat - in this edit I extend the question: If I use class I suppose it should be like this:

class Player
{
    min:KnockoutObservable<number>;
    constructor(min:number=0)
    {
        this.min=ko.observable(min);
    }
}

BUT how should computed be defined?

like image 990
Rune Jeppesen Avatar asked Jan 14 '23 01:01

Rune Jeppesen


1 Answers

You can use computed with generics (latest Typescript 0.9), just define type in declaration and in constructor you will assign value to result of call to ko.computed:

export class Inbox extends vm.BriskIdeaViewModel {

    public rapidEntryText = ko.observable<string>();
    public todosActive: KnockoutComputed<Array<ITodo>>;

    constructor() {
        super();
        this.todosActive = ko.computed(() => {
            return _.filter(this.dataContext.todos(), x => !x.isDone());
        });
    }
}
like image 185
nihique Avatar answered Jan 19 '23 10:01

nihique