I have a model with two property: title and content and what I want to do is:
If title has a value, use it but in case it’s blank use the first 20 chars + “…” from content.
This is the model:
function Note(title, content) {
var self = this;
self.content = ko.observable(content);
self.title = ko.computed({
read: function(){
if(!title){
var content = self.content();
if(content) return content.substring(0,19) + "...";
}
},
write: function(title){
return title;
}
});
}
Title value gets correctly updated from content but it’s impossible (for me) to get writing directly on title working..
The only problem in RP Niemeyer answer is that i must have only on property for reading/writing, is that possible?
When creating your writeable computed observable, you will want to have a separate observable to contain the actual title.
More like: