2

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

我需要在打字稿中编写以下代码。

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 的正确方法吗?

4

1 回答 1

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));
于 2015-01-26T11:05:22.807 回答