简体   繁体   English

Emberjs和TypeScript-计算属性

[英]Emberjs and TypeScript - computed property

I'm currently trying to develop some EmberJs + TypeScript prototype. 我目前正在尝试开发一些EmberJs + TypeScript原型。 I'm using this tutorial and I'm stuck at this point . 我使用这个教程和我被困在这 How should I create computed property in TypeScript? 如何在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. 您不能使用typescript类语法来扩展Ember类。 You have to use the extend method as you would in normal Javascript. 您必须像在普通Javascript中一样使用extend方法。 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). 原因是Ember所做的不只是扩展类的原型(您可以从计算属性无法正常工作中看到)。

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. 如果你真的想使用类语法,我建议要么使用EmberScript这是灰烬建,或SweetJS建立一个宏来完成它。 Unfortunately, neither will work very well with Typescript. 不幸的是,这两种类型都不适合与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. 或者,如果您感觉特别聪明,可以修改Typescript生成的__extends方法,并可能以这种方式实现您想要的。

But it all seems to be overkill for some syntactic sugar. 但是,对于某些语法糖来说,这一切似乎都是过大的。 Just use the extend method that Ember provides. 只需使用Ember提供的extend方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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