这是小提琴。
我有一个视图模型,其中部分数据将在一些用户交互后可用。
既然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.
});