//返回到經(jīng)典的"first name +last name= full name"例子上粥喜,你可以讓事情調(diào)回來看:讓自動屬性fullName可寫,讓用戶直接輸入姓名全稱橘券,
//然后輸入的值將被解析并并映射寫入到基本的監(jiān)控屬性 firstName 和 lastName 上:
<pre>
<code>
function MyViewModel()
{
this.firstName =ko.obsetrvable("Plant");
this.lastName = ko.observable("Earth");
this.fullName =ko.compute({
read:function(){
return this.firstName()+" "+this.lastName();
},
write:function(value)
{
var lastSpacePos = value.lastIndexOf(" ");
if(lastSpacePos > 0) { //Ignore values with no space characterSet
this.firstName(value.substring(0,lastSpacePose));// Update"firstName"
this.firstName(value.substring(0,lastSpacePos));// Update"lastName"
}
},
owner:this
});
}
ko.applyBindings(new MyViewModel());
//這個例子里,寫操作的callback接受寫入的值额湘,把值分離出來,分別寫入"firstName"和"lastName"上、你可以想普通情況一樣將
//這個view model綁定到dom元素上
<p> First name:<span data-bind="text:firstName"></span></p>
<p> Last name:<span data-bind="text:lastName"></span></p>
<h2>Hello,<input data-bind="value:fullName"/>!</h2>
<code>