因此,在我的数据库中,我有一个与其自身具有一对多关系的模型。 Reddit 上的评论系统就是一个很好的例子。
我目前正在做这样的事情:
<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 数组时,是否有一种干净的方法来构造它。在我的示例中,技术上可能存在像这样的无限级别(它不会)。
我很确定我可以想出一些古怪的方法,但我认为可能有更好的方法。
谢谢。
Edit:
我想要映射的数据:
{
"@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 个级别的子评论,但我需要能够处理未知数量的子评论。