Meteor:显示带把手的客户端嵌套对象

时间:2016-02-26 09:16:32

标签: javascript object meteor nested handlebars.js

我有来自助手的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 }中进行迭代时,我很难在手柄中丢失,但是当我必须在嵌套在其他对象中的对象中进行迭代时,我无法做到:<\ n / p>

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

我试着得到这样的结果:

  • rA5s5jSz7q9ZSCcNJ
  • 图库

  • ImageUpload

    • addImage:true
    • modifyImage:false
    • removeImage:false
  • 文章
    • readArticle:true
    • createArticle:false
    • modifyArticle:false

1 个答案:

答案 0 :(得分:0)

您希望使用#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}}

希望有所帮助!