【发布时间】:2015-08-11 08:52:31
【问题描述】:
我有一个 posts 的 observableArray。每个帖子都应该有其 observableArray of cmets,我正在这样做:
self.posts.subscribe(posts) {
ko.utils.arrayForEach(posts, function(post) {
post.comments = ko.observableArray()
})
}
所以我有两个 foreach:
<!-- ko foreach: posts -->
<div class="post">
...
<!-- ko forech: comments -->
<div class="comment">
<span class="delete_comment" data-bind="click: $root.deleteComment"></span>
</div>
<!-- /ko -->
<!-- /ko -->
在我的视图模型中,deleteComment 函数:
self.deleteComment = function(comment) {
//ajax..
// now i should remove this comment from the comments array
}
这里的问题是我找不到从 cmets 数组中删除注释的方法。我无法从视图模型访问 cmets 数组,因为它是动态创建的。我试图在数据绑定中绑定父级:
<span class="delete_comment" data-bind="click: $root.deleteComment.bind($parent)"></span>
但是没有区别,deleteComment 中的第一个参数仍然是评论对象。如何从deleteComment 内部访问外部 observableArray?
【问题讨论】: