Si consiglia di creare proprietà calcolate sull'oggetto prototipo?Utilizzo del prototipo per proprietà calcolate KnockoutJS
Questo è quello che ho tentato di seguito ma il binding firstName
sta restituendo la funzione come una stringa anziché eseguirla (http://jsfiddle.net/W37Yh).
var HomeViewModel = function(config, $, undefined) {
if (!this instanceof HomeViewModel) {
return new HomeViewModel(config, $, undefined);
}
this.firstName = ko.observable(config.firstName);
this.lastName = ko.observable(config.lastName);
};
HomeViewModel.prototype.fullName = function() {
return ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
};
var model = new HomeViewModel({
firstName: "John",
lastName: "Smith"
}, jQuery);
ko.applyBindings(model);
No, la creazione di oggetti basata su prototipi e l'eliminazione diretta di solito non vanno bene insieme. – Niko
I secondo quello che ha detto niko, il problema con il prototipo è che tutte le istanze dei tuoi oggetti cercheranno di condividere lo stesso calcolato sul prototipo. Molto brutto. La cosa immediata che stai vedendo è che il nome completo è solo una funzione che restituisce un calcolo, non un calcolo, quindi la stringa. –