Ember js观察者和打字稿中的计算属性

时间:2015-01-26 09:04:39

标签: javascript ember.js typescript ember-data typescript1.4

如何使用typescript定义emberjs观察者和计算属性?

我需要在typescript中编写以下代码。

module App {
App.UserController = ember.ObjectController.extend({
    firstName: "John",
    lastName: "Doe",
    fullName: function() {
       return this.get("firstName") + " " + this.get("lastName");
    }.property("firstName", "lastName")
});
}

类似的东西:

class UserController extends ember.ObjectController {
    firstName: string;
    lastName: string;

    constructor() {
        super();
        this.firstName = "John";
        this.lastName = "Doe";
    }

    get fullName(): string {
        return this.firstName + " " + this.lastName;
    }.property("firstName", "lastName")
}

但这似乎不起作用。有人可以在不使用javascript的情况下告诉我正确的方法吗?

1 个答案:

答案 0 :(得分:2)

以下是将编译的代码版本:

module App {

    export var UserController = Ember.ObjectController.extend({
        firstName: "John",
        lastName: "Doe",
        fullName: function () {
            return this.get("firstName") + " " + this.get("lastName");
        }.property('model.isCompleted')
    });
}

// Example:
var x = App.UserController;

更新

您可以使用类来实现它,这是一个编译的示例:

class UserController extends Ember.ObjectController {
    private firstName: string;
    private lastName: string;

    constructor() {
        super();
        this.firstName = "John";
        this.lastName = "Doe";
    }

    get fullName(): string {
        return this.firstName + " " + this.lastName;
    }
}

var x = new UserController();

console.log(JSON.stringify(x));