Computed data properties on prototype, using constructor
HTML:
<!-- data-linking-->
<input data-link="fullName()" />
...
<td data-link="fullName(true)" ></td>
<!-- data-bound tag-->
{^{:fullName(true)}}
Script:
//====================== Data ======================
// Constructor
var Person = function(first, last) {
this._firstName = first;
this.lastName = last;
},
// Prototype
personProto = {
// Computed firstName
firstName: function() { ... },
// Computed fullName
fullName: fullName
};
//Setter for firstName - for two-way binding
personProto.firstName.set = function(val) { ... }
Person.prototype = personProto,
person = new Person("Jeff", "Friedman");
// Parameterized computed observable
function fullName(reversed) {
return reversed ? this.lastName + " " + this.firstName ...
}
// Declare dependencies
fullName.depends = ["firstName", "lastName"];
// For two-way binding of computed observables, provide a setter
fullName.set = function(val) {
$.observable(this).setProperty({
lastName: ...,
firstName: ...
});