【发布时间】:2016-05-22 12:47:26
【问题描述】:
我正在将 Knockout 与 TypeScript 相结合。考虑这个例子:
class Person
{
public FirstName:string = "John";
public LastName: string = "Doe";
public get FullName(): string
{
return this.FirstName + " " + this.LastName;
}
public set FullName(fullName: string): void
{
var names = fullName.split(" ");
this.FirstName = names[0];
this.LastName = names[1];
}
}
翻译成:
var Person = (function()
{
function Person()
{
this.FirstName = "John";
this.LastName = "Doe";
}
Object.defineProperty(
Person.prototype,
"FullName",
{
get: function()
{
return this.FirstName + " " + this.LastName;
},
set: function(fullName)
{
var names = fullName.split(" ");
this.FirstName = names[0];
this.LastName = names[1];
},
enumerable: true,
configurable: true
});
return Person;
})();
调查:
var mapped = ko.mapping.fromJS(new Person());
mapped.FirstName("Steve");
console.log("Want 'Steve Doe', was :", mapped.FullName()); //John Doe
mapped.FullName("John Travolta");
console.log("Want 'Travolta', was :", mapped.LastName()); //Doe
这根本不起作用。 是否可以让 ko.mapping 以通用的可重用方式理解 getter 和 setter?
【问题讨论】:
标签: knockout.js typescript knockout-mapping-plugin