これがフィドルです。
ユーザーの操作後にデータの一部が利用可能になるビューモデルがあります。
はapplyBindings
ドキュメントの準備ができた時点で既に呼び出されているのに、ボタン クリック イベント中にもう一度呼び出す必要があるのはなぜですか?
HTML:
<p>first name:
<input type="text" data-bind="value: ray.firstName" />
</p>
<p>last name:
<input type="text" data-bind="value: ray.lastName" />
</p>
<p>first name:
<input type="text" data-bind="value: joe.firstName" />
</p>
<p>last name:
<input type="text" data-bind="value: joe.lastName" />
</p>
JS:
function person(firstNm, lastNm) {
this.firstName = firstNm;
this.lastName = lastNm;
}
function myApp() {
this.ray = new person();
this.joe = new person();
}
var app = new myApp();
app.ray = new person('ray', 'cheng');
ko.applyBindings(app);
$('#showName').on('click', function () {
app.joe = new person('joe', 'public');
ko.applyBindings(app); // <-- why is this needed since applyBindings already called above.
});