Exploring the fusion of Knockout and TypeScript. Check out this code snippet:
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];
}
}
This can be transformed into:
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;
})();
Experiment further with:
var mapped = ko.mapping.fromJS(new Person());
mapped.FirstName("Steve");
console.log("Expected 'Steve Doe', got :", mapped.FullName()); //John Doe
mapped.FullName("John Travolta");
console.log("Expected 'Travolta', got :", mapped.LastName()); //Doe
However, there seems to be an issue. Is there a way for ko.mapping to comprehend getters and setters in a versatile manner?