剔除绑定了一对多的自我关系

剔除绑定了一对多的自我关系

本文介绍了剔除绑定了一对多的自我关系(剔除递归)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在我的数据库中,我有一个与自身具有一对多关系的模型.一个很好的例子是在reddit上的评论系统.

So in my database I have a model that has a one to many relationship with itself. A good example of this is a comments system like on reddit.

我目前正在做这样的事情:

I currently am doing something like this:

<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>

显然仅支持一个级别的儿童.当Child(Children [i])可能有也可能没有需要遍历的Children数组时,是否有一种干净的方法来构造它.在我的示例中,从技术上讲,可能会有这样的无限级别(不会).

which obviously only supports one level of children.Is there a clean way to structure this when a Child (Children[i]) may or may not also have a Children array that needs to be looped over. In my example there could technically be infinite levels like this (it won't be).

我很确定我可以提出一些棘手的问题,但我认为可能会有更好的方法.谢谢.

I'm pretty sure I could come up with something hacky but I think there may be a better way.Thanks.

我要映射的数据:

{
   "@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
   "SectionID":4,
   "Text":"Text",
   "Html":null,
   "OrderQualifier":"1",
   "IsUserCreated":false,
   "Children":[
      {
         "@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
         "SectionID":4,
         "Text":"Text",
         "Html":null,
         "OrderQualifier":"1",
         "IsUserCreated":false,
         "Children":[
            {
               "@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
               "SectionID":4,
               "Text":"Text",
               "Html":null,
               "OrderQualifier":"1",
               "IsUserCreated":false,
               "Children":[
                  {
                     "@odata.context":"http://localhost:3080/odata/$metadata#SectionApi(*)/$entity",
                     "SectionID":4,
                     "Text":"Text",
                     "Html":null,
                     "OrderQualifier":"1",
                     "IsUserCreated":false,
                     "Children":[

                     ]
                  }
               ]
            }
         ]
      }
   ]
}

如您所见,它包含3个级别的子注释,但我需要能够处理未知数量的子注释.

As you can see this contains 3 levels of child comment but I need to be able to handle an unknown number of child comments.

推荐答案

Knockout模板支持递归. http://jsfiddle.net/m812qjeq/2/

knockout template supports recursion. http://jsfiddle.net/m812qjeq/2/

<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>

这篇关于剔除绑定了一对多的自我关系(剔除递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:07