1
我在與Knockout.JS
例如,如果我有以下中的發言權文件app.js嵌套綁定問題
:
var UserModel = function() {
this.writeups = ko.observableArray([]);
}
var WriteupModel = function() {
this.type = 'some type';
}
var MyViewModel = function() {
this.newUser = new UserModel();
this.selectedUser = ko.observable(this.newUser);
this.selectedUser().writeups().push(new WriteupModel());
}
ko.applyBindings(new MyViewModel());
及以下對於視圖:
<div id="empReportView" data-bind="template: { name: 'empTmpl', data: selectedUser }"></div>
<script type="text/html" id="empTmpl">
<table>
<tbody data-bind="template: { name: 'empWuItem', foreach: $data.writeups } ">
</tbody>
</table>
</script>
<script type="text/html" id="empWuItem">
<tr>
<td data-bind="text: type"></td>
</tr>
</script>
每當將另一個WriteupModel壓入屬於selectedUser的writeups數組時,表不會更新。這是我試圖完成的一個簡化版本,但是應該假定當他們創建一個文件時,它應該根據新的信息更新這個文件。
我是Knockout的新手,所以任何幫助將不勝感激!
謝謝。
- = - =編輯1 = - = -
有一點需要注意,如果你重裝了selectedUser它會吐出所添加的書面記錄的empWuItem模板的結合。這看起來效率不高,因爲WriteUp被添加到UserModel中的writeups可觀察數組而不必在視圖模型中「重新分配」selectedUser屬性時觸發綁定。
完美!這很好。謝謝您的幫助。 – Aric