【问题标题】:Meteor : display with handlebars client side nested object流星:显示与把手客户端嵌套对象
【发布时间】:2023-03-09 23:40:01
【问题描述】:

我有来自助手的 JSON:

 {
  "perms": [
     {
         "userId": "rA5s5jSz7q9ZSCcNJ",
         "perms": [
             {
                 "moduleName": "Gallery",
                 "container": {
                     "ImageUpload": {
                         "addImage": false,
                         "modifyImage": false,
                         "removeImage": false
                     },
                     "Article": {
                         "readArticle": false,
                         "createArticle": false,
                         "modifyArticle": false,
                         "removeArticle": false,
                         "archiveArticle": false
                     }
                 }
             }
         ]
     },
     {
         "userId": "RrmynmmngJEMsRRpk",
         "perms": [
             {
                 "moduleName": "Gallery",
                 "container": {
                     "ImageUpload": {
                         "addImage": false,
                         "modifyImage": false,
                         "removeImage": false
                     },
                     "Article": {
                         "readArticle": false,
                         "createArticle": false,
                         "modifyArticle": false,
                         "removeArticle": false,
                         "archiveArticle": false
                     }
                 }
             }
         ]
     }
 ]

我的 JS 是:

    'userWithRights':function() {
        Meteor.call('genereObjectPermission',function(err, resp){
           Session.set('responseServer', resp);
           });
        responseServer = Session.get('responseServer')
        return _.map(responseServer, function(value, key) { return {key: key, value: value}; })

    },'iterateInValue':function(){
        return _.map(this, function(value, key) { return {key: key, value: value}; })

}

还有我的 HTML 代码(多次试验的结果):

{{#each userWithRights}}
  <p> {{key}} </p>
  {{#each value}}
    <li>{{this.userId}}</li>
    {{#each perms}}
      <li><li>{{moduleName}}</li></li>
      {{#each test58}}
          <li><li><li>{{key}}</li></li></li
      {{/each }}
    {{/each}}
  {{/each}}
{{/each}}

所以,当我必须在 Object { key : value } 中进行迭代时,我有点迷失了方向,但当我必须在嵌套在其他对象中的对象中进行迭代时,我不能:

{"container": { "ImageUpload": { "removeImage": false }}}

我尝试得到这样的结果:

  • rA5s5jSz7q9ZSCcNJ
  • 图库

  • 图片上传

    • addImage : 真
    • 修改图像:假
    • removeImage:假
  • 文章
    • readArticle : 是的
    • createArticle : 错误
    • 修改文章:假

【问题讨论】:

    标签: javascript object meteor nested handlebars.js


    【解决方案1】:

    您想使用#with 助手并使用@key 访问密钥:

    {{#each userWithRights}}
      <p> {{key}} </p>
      <ul>
      {{#each value}}
        <li>{{this.userId}}</li>
        {{#each perms}}
          <li>{{moduleName}}</li>
          {{#with container}}
            {{#with ImageUpload}}
              <li>{{@key}}</li>
            {{/with}}
            <ul>
            {{#each ImageUpload}}
              <li>{{@key}}: {{this}}</li>
            {{/each}}
            </ul>
            {{#with Article}}
              <li>{{@key}}</li>
            {{/with}}
            <ul>
            {{#each Article}}
              <li>{{@key}}: {{this}}</li>
            {{/each}}
            </ul>
          {{/with}}
        {{/each}}
      {{/each}}
      </ul>
    {{/each}}
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 2013-02-08
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      相关资源
      最近更新 更多