简体   繁体   中英

Emberjs and TypeScript - computed property

I'm currently trying to develop some EmberJs + TypeScript prototype. I'm using this tutorial and I'm stuck at this point . How should I create computed property in TypeScript?

I've tried something like this, but it doesn't work:

module App {
export class TodoController extends Em.ObjectController {

    constructor() {
        super();
    }

    public isCompleted = ((key, value) => {
        var model = this.get('model');
        if (value === undefined) {
            // property being used as a getter
            return model.get('isCompleted');
        } else {
            // property being used as a setter
            model.set('isCompleted', value);
            //model.save();
            return value;
        }
    }).property('model.isCompleted');
}
}

You can't use the typescript class syntax to extend an Ember class. You have to use the extend method as you would in normal Javascript. The reason being is that Ember does more than just extend the prototype of the class (as you can see from your computed property not working).

If you really want to use the class syntax, I suggest either using EmberScript which is built for Ember, or SweetJS to build a macro to do it. Unfortunately, neither will work very well with Typescript.

Or, if you're feeling particularly clever, you could modify the __extends method that Typescript generates and possibly achieve what you want that way.

But it all seems to be overkill for some syntactic sugar. Just use the extend method that Ember provides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM