首页 \ 问答 \ 如何在Meteor中呈现嵌套集合?(How to render nested collections in Meteor?)

如何在Meteor中呈现嵌套集合?(How to render nested collections in Meteor?)

概要:
嵌套在父类别中的子类别不会在流星模板中呈现。

细节:
考虑一下'Category'的数据模型:

// Model Schema
Category {
   idCategory : 20, (id of the category itself)
   idCategoryParent : 0, (idCategory of our parent category)
   defaultLabel : "Movies" (our label)
}

有父类别和子类别。 父类别的idCategoryParent属性值为0.子类别将其父类的idCategory作为它们的idCategoryParent属性进行存储。 我试图循环访问这些类别的一个集合,并按以下方式呈现它们:

<b>Movies</b> // parent category is in bold
<ul> // child categories are rendered as an unordered list
  <li>Horror</li> 
  <li>Comedy</li>
  <li>Action</li>
  <li>Drama</li>
</ul>

<b>Music</b>
<ul>
  <li>Rock</li> 
  <li>Classical</li>
  <li>Ambient</li>
</ul>

但是,这是我实际得到的:

<b>Movies</b>
<ul> // empty...
</ul>

<b>Music</b>
<ul>
</ul>

源代码:

// How we get the 'categories_parents' data
Template.content.categories_parents = function (){ 

    /*
    * Get all parent categories (categories with an idCategoryParent of 0)
    */
    var parents = Categories.find({idCategoryParent:0});
    var pCount = parents.count();

    for (var i = 0; i < pCount; i++){

            var pId = parents.db_objects[i]['idCategory'];
            /* 
            * Get all child categories of the parent (categories with
            * an idCategoryParent equal to value of parent category's idCategory).
            */
            var children = Categories.find({idCategoryParent:pId});
            var cCount = children.count();

            /*
            * Assign the child categories array as a property of the parent category
            * so that we can access it easily in the template #each expression
            */
            parents.db_objects[i]['children'] = children;
    }

    return parents;
}


// This is our template
<template name="content">
<h1>Categories</h1>
    {{#each categories_parents}}
        <b>{{defaultLabel}}</b><br />
        <ul>
            {{#each children}}
            <li>{{defaultLabel}}</li>
            {{/each}}
        </ul>
    {{/each}}
</template>

其他模板配置我曾尝试进行故障排除:

{{#each children}}
<li>A Child Exists Here</li> // Even this never rendered... no children?
{{/each}}

任何线索,为什么发生这种情况将不胜感激。


Summary:
Child categories nested inside of Parent Categories are not getting rendered in a Meteor template.

Details:
Consider a data model for 'Category' as such:

// Model Schema
Category {
   idCategory : 20, (id of the category itself)
   idCategoryParent : 0, (idCategory of our parent category)
   defaultLabel : "Movies" (our label)
}

There are parent categories and child categories. Parent categories have an idCategoryParent property value of 0. Child categories store the idCategory of their parents as their idCategoryParent property. I'm trying to loop through a collection of these Categories and render them in the following way:

<b>Movies</b> // parent category is in bold
<ul> // child categories are rendered as an unordered list
  <li>Horror</li> 
  <li>Comedy</li>
  <li>Action</li>
  <li>Drama</li>
</ul>

<b>Music</b>
<ul>
  <li>Rock</li> 
  <li>Classical</li>
  <li>Ambient</li>
</ul>

However, this is what I actually get:

<b>Movies</b>
<ul> // empty...
</ul>

<b>Music</b>
<ul>
</ul>

Source Code:

// How we get the 'categories_parents' data
Template.content.categories_parents = function (){ 

    /*
    * Get all parent categories (categories with an idCategoryParent of 0)
    */
    var parents = Categories.find({idCategoryParent:0});
    var pCount = parents.count();

    for (var i = 0; i < pCount; i++){

            var pId = parents.db_objects[i]['idCategory'];
            /* 
            * Get all child categories of the parent (categories with
            * an idCategoryParent equal to value of parent category's idCategory).
            */
            var children = Categories.find({idCategoryParent:pId});
            var cCount = children.count();

            /*
            * Assign the child categories array as a property of the parent category
            * so that we can access it easily in the template #each expression
            */
            parents.db_objects[i]['children'] = children;
    }

    return parents;
}


// This is our template
<template name="content">
<h1>Categories</h1>
    {{#each categories_parents}}
        <b>{{defaultLabel}}</b><br />
        <ul>
            {{#each children}}
            <li>{{defaultLabel}}</li>
            {{/each}}
        </ul>
    {{/each}}
</template>

Other template configurations I have tried in troubleshooting:

{{#each children}}
<li>A Child Exists Here</li> // Even this never rendered... no children?
{{/each}}

Any clues as to why this is happening would be appreciated.


原文:https://stackoverflow.com/questions/13750854
更新时间:2021-10-02 10:10

最满意答案

查询的fetch参数可能很棘手。 如果提供fetch=true您将获得查询类型(Story,Defect)上存在的所有字段。 如果该字段也是域对象(如任务或缺陷),您将只获得这样的精简ref对象

    {
       "_ref": "/task/1234.js"  
    }

如果要在子对象上填充字段,则需要指定要在fetch param fetch=Name,FormattedID,Tasks显示的字段。 这将返回如下所示的对象:

    {
      "HierarchicalRequirement" : {
        "Name" : "StoryName",
        "FormattedID" : "S1234",
        "Tasks" : [
          {
            "_rallyAPIMajor": "1",
            "_rallyAPIMinor": "26",
            "_ref": "https://rally1.rallydev.com/slm/webservice/1.26/task/9872916743.js",
            "_refObjectName": "Update XYZ when ABC",
            "_type": "Task",
            "FormattedID" : "T1",
            "Name" : "Update XYZ when ABC"
          }]

}

如果有帮助,请告诉我


The fetch parameter can be tricky with queries. If you provide fetch=true you will get all of the fields that exist on the queried type (Story,Defect). If the field is also a domain object (like a tasks or a defect) you will only get the thin ref object like this

    {
       "_ref": "/task/1234.js"  
    }

If you want to get fields populated on the sub-objects you will need to specify the fields you want shown in the fetch param fetch=Name,FormattedID,Tasks. This would return an object like the one below:

    {
      "HierarchicalRequirement" : {
        "Name" : "StoryName",
        "FormattedID" : "S1234",
        "Tasks" : [
          {
            "_rallyAPIMajor": "1",
            "_rallyAPIMinor": "26",
            "_ref": "https://rally1.rallydev.com/slm/webservice/1.26/task/9872916743.js",
            "_refObjectName": "Update XYZ when ABC",
            "_type": "Task",
            "FormattedID" : "T1",
            "Name" : "Update XYZ when ABC"
          }]

}

Let me know if that helped

相关问答

更多

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)