我一直在使用ko.obsevableArray和ko模板通过ko.applyBindings(mww.vm) - ViewModel - 迭代并显示:
mww.vm = (function () {
var
loadWishlists = function () {
mww.vm.wishlists.removeAll();
$.each(mww.sampleData.data.wishlists, function (i, w) {
mww.vm.wishlists.push(new mww.Wishlist()
.WishlistId(w.WishlistId)
.Name(w.Name)
.Ocassion(w.Ocassion)
.Date(w.Date)
);
});
}
addWishlist = function () {
mww.vm.wishlists.push(new mww.Wishlist()
.WishlistId(0)
.Name($('#NameTxt').val())
.Ocassion($('#OccassionTxt').val())
.Date($('DateTxt').val())
);
};
return {
wishlists: ko.observableArray([]),
friends: ko.observableArray([]),
loadWishlists: loadWishlists,
addWishlist: addWishlist
};
})();
模板:
<div id="WishlistTempl" style="display:none;">
<div class="WishlistBox">
<div class="wwBox">
<div class="BoxHeading">
<div class="ButtonPanel">
<span data-bind="text:Name"></span>
</div>
<div class="FriendPicker" data-bind="template: {name:'FriendTmpl', foreach:$parent.friends}"></div>
</div>
</div>
它有效,但是当我使用addWishlist,在wishlists ko.observableArray中推送一个新项目时,Wishlist的属性显示在模板中,但不显示在$ parent.friends中。有什么想法吗?