javascript - Typescript'te Ember js gözlemcileri ve hesaplanmış özellikler


Emberjs gözlemcilerini ve hesaplanmış özelliklerini TypeScript kullanarak nasıl tanımlayabilirim?

Aşağıdaki kodu TypeScript'te yazmam gerekiyor.

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

Şun gibi:

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")
}

Ama bu işe yaramıyor gibi görünüyor. Biri bana bunu javascript kullanmadan doğru şekilde yapmanın yolunu söyleyebilir mi?

Cevap #1

İşte derlenecek olan kodunuzun bir sürümü:

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;

Güncelle

Bunu bir sınıf ile uygulayabilirsiniz, işte derlenen bir örnek:

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));