Knockout.js 敲除绑定自身上的一对多关系(敲除中的递归)
所以在我的数据库中,我有一个模型,它与自身有一对多的关系。一个很好的例子是reddit上的评论系统 我目前正在做这样的事情:Knockout.js 敲除绑定自身上的一对多关系(敲除中的递归),knockout.js,many-to-one,Knockout.js,Many To One,所以在我的数据库中,我有一个模型,它与自身有一对多的关系。一个很好的例子是reddit上的评论系统 我目前正在做这样的事情: <div class="body" data-bind="foreach: { data: Comments}"> <span data-bind="text: '(' + OrderQualifier + ') ' + Text"></span> <!-- ko foreach: { data: Children
<div class="body" data-bind="foreach: { data: Comments}">
<span data-bind="text: '(' + OrderQualifier + ') ' + Text"></span>
<!-- ko foreach: { data: Children } -->
<span data-bind="text: '(' + OrderQualifier + ') ' + Text"></span>
<!-- /ko -->
</div>
正如您所见,这包含3个级别的子注释,但我需要能够处理未知数量的子注释。淘汰模板支持递归
您可以为此使用模板
<div class="body" data-bind="foreach: comments">
<div data-bind="template: { name: 'childTemplate', data: $data }"></div>
</div>
<script type="text/html" id="childTemplate">
<span data-bind="text:comment"></span>
<!-- ko if: $data.childrenLength > 0 -->
<!-- ko foreach: children -->
<div data-bind="template: { name: 'childTemplate', data: $data }" style="padding-left:35px;"></div>
<!-- /ko -->
<!-- /ko -->
</script>
如果你发布一些样本数据,最好仔细阅读一下,这正是我要做的
<div class="body" data-bind="foreach: Comments">
<div data-bind="template: { name: 'childTemplate', data: $data }"></div>
</div>
<script type="text/html" id="childTemplate">
<span data-bind="text: '(' + OrderQualifier + ') ' + Text"></span>
<!-- ko if: $data.Children -->
<!-- ko foreach: Children -->
<div data-bind="template: { name: 'childTemplate', data: $data }"></div>
<!-- /ko -->
<!-- /ko -->
</script>
<div class="body" data-bind="foreach: comments">
<div data-bind="template: { name: 'childTemplate', data: $data }"></div>
</div>
<script type="text/html" id="childTemplate">
<span data-bind="text:comment"></span>
<!-- ko if: $data.childrenLength > 0 -->
<!-- ko foreach: children -->
<div data-bind="template: { name: 'childTemplate', data: $data }" style="padding-left:35px;"></div>
<!-- /ko -->
<!-- /ko -->
</script>
var comments = [{
id: 1,
comment: 'How can i use knockoutjs?',
childrenLength: 3,
children: [{
id: 2,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}, {
id: 3,
comment: 'Please read the documentation',
childrenLength: 0,
children: []
}, {
id: 4,
comment: 'You can see the blog posts on this',
childrenLength: 2,
children: [{
id: 5,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}, {
id: 6,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}]
}]
}, {
id: 7,
comment: 'You question is not sufficient to be asked here?',
childrenLength: 3,
children: [{
id: 8,
comment: 'Please seach before asking',
childrenLength: 0,
children: []
}, {
id: 9,
comment: 'Please read the documentation',
childrenLength: 0,
children: []
}, {
id: 10,
comment: 'You can see the blog posts on this',
childrenLength: 0,
children: []
}]
}]
var vm = function(){
var self = this
self.comments = ko.observableArray(comments)
}
$('document').ready(function () {
ko.applyBindings(new vm())
})