首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在工具栏表达式中查找JSON对象

如何在工具栏表达式中查找JSON对象
EN

Stack Overflow用户
提问于 2014-12-06 06:35:29
回答 1查看 314关注 0票数 1

我正在使用工具栏,并试图使用post的userId来查找用户的用户名和图片;目前,我正在使用userId作为正确表达式的占位符。我还尝试过几种方法从JSON文件中获取数据,而不是内联的,但我没有做到这一点。

代码语言:javascript
复制
{{#posts}} 
<div class="post">
    <div class="avatar">
        <img src="{{userId}}">
    </div>
    <div class="post-content">
        <a href="#">{{userId}}</a>
        {{content}}
    </div>  
    <div class="post-comments">
        {{#comments}} 
        <div class="comment">
            <div class="avatar">
                <img src="{{userId}}">
            </div>
            <a href="#">{{userId}}</a>
            {{content}}
        </div>
        {{/comments}} 
        <input type="text" name="comment" placeholder="post a comment"/>
    </div>              
</div>
{{/posts}} 

var source = $("#postTemplate").html(); 
var template = Handlebars.compile(source);
var data = { 
posts: [
{
  "id": 1,
  "userId": 1,
  "content":"Love wine? Love food?",
  "comments": [
    {
      "id": 13,
      "postId": 1,
      "userId": 2,
      "content": "Would you happen to know were Capone is? "
    }
  ]
},
{
  "id": 2,
  "userId": 2,
  "content":"Day 2 of house sitting...awww my firends really do Trust me!",
  "comments": []
}
],
users: [
{
  "id": 1,
  "username": "James Bond",
  "pic": "images/profile/Sean-Connery-as-James-Bond.jpg",
},
{
  "id": 2,
  "username": "William Forrester",
  "pic": "images/profile/2001_finding_forrester_008.png",
}
]
}; 
$('.feed').append(template(data));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-06 09:13:09

如果将用户数组转换为键为userId的对象,则如下所示:

代码语言:javascript
复制
users: {
      "1": {
              "username": "James Bond",
              "pic": "http://i49.tinypic.com/28vepvr.jpg",
           },
     "2": {
            "username": "William Forrester",
            "pic": "http://i46.tinypic.com/2epim8j.jpg",
      }
}

然后您可以创建两个自定义帮助程序,如下所示:

代码语言:javascript
复制
var data = {
  posts: [{
    "id": 1,
    "userId": 1,
    "content": "Love wine? Love food?",
    "comments": [{
      "id": 13,
      "postId": 1,
      "userId": 2,
      "content": "Would you happen to know were Capone is? "
    }]
  }, {
    "id": 2,
    "userId": 2,
    "content": "Day 2 of house sitting...awww my firends really do Trust me!",
    "comments": []
  }],
  users: {
    "1": {
      "username": "James Bond",
      "pic": "http://i49.tinypic.com/28vepvr.jpg",
    },
    "2": {
      "username": "William Forrester",
      "pic": "http://i46.tinypic.com/2epim8j.jpg",
    }
  }
}
Handlebars.registerHelper('username', function(options) {
  var userInfo = options.data.root.users[this.userId];
  return userInfo ? userInfo.username : "";
});
Handlebars.registerHelper('avatar', function(options) {
  var userInfo = options.data.root.users[this.userId];
  return userInfo ? userInfo.pic : "";
});
var source = $("#postTemplate").html();
var template = Handlebars.compile(source);
$('.feed').append(template(data));
代码语言:javascript
复制
.post{
  margin:5px;
}
.post-comments{
  padding-left:20px;
}
img {
  width: 50px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.min.js"></script>
<script type="text/template" id="postTemplate">
  {{#each posts}}
  <div class="post">
    <div class="avatar">
      <img src="{{avatar}}">
    </div>
    <div class="post-content">
      <a href="#">{{username}}</a>
      {{content}}
    </div>
    <div class="post-comments">
      {{#each comments}}
      <div class="comment">
        <div class="avatar">
          <img src="{{avatar}}">
        </div>
        <a href="#">{{username}}</a>
        {{content}}
      </div>
      {{/each}}
      <input type="text" name="comment" placeholder="post a comment" />
    </div>
  </div>
  {{/each}}
</script>
<div class="feed"></div>

或者,您可以保留数组并对其进行迭代,直到在帮助器中找到匹配为止,这要花费很大的代价:

代码语言:javascript
复制
var data = {
  posts: [{
    "id": 1,
    "userId": 1,
    "content": "Love wine? Love food?",
    "comments": [{
      "id": 13,
      "postId": 1,
      "userId": 2,
      "content": "Would you happen to know were Capone is? "
    }]
  }, {
    "id": 2,
    "userId": 2,
    "content": "Day 2 of house sitting...awww my firends really do Trust me!",
    "comments": []
  }],
  users: [{
    "id": 1,
    "username": "James Bond",
    "pic": "http://i50.tinypic.com/f0ud01.jpg",
  }, {
    "id": 2,
    "username": "William Forrester",
    "pic": "http://i49.tinypic.com/28vepvr.jpg",
  }]
};
Handlebars.registerHelper('username', function(options) {
  var id = this.userId,
    userInfo = $.grep(options.data.root.users, function(userInfo) {
      return userInfo.id == id;
    })[0];
  return userInfo ? userInfo.username : "";
});
Handlebars.registerHelper('avatar', function(options) {
  var id = this.userId,
    userInfo = $.grep(options.data.root.users, function(userInfo) {
      return userInfo.id == id;
    })[0];
  return userInfo ? userInfo.pic : "";
});
var source = $("#postTemplate").html();
var template = Handlebars.compile(source);
$('.feed').append(template(data));
代码语言:javascript
复制
.post {
  margin: 5px;
}
.post-comments {
  padding-left: 20px;
}
img {
  width: 50px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.min.js"></script>
<script type="text/template" id="postTemplate">
  {{#each posts}}
  <div class="post">
    <div class="avatar">
      <img src="{{avatar}}">
    </div>
    <div class="post-content">
      <a href="#">{{username}}</a>
      {{content}}
    </div>
    <div class="post-comments">
      {{#each comments}}
      <div class="comment">
        <div class="avatar">
          <img src="{{avatar}}">
        </div>
        <a href="#">{{username}}</a>
        {{content}}
      </div>
      {{/each}}
      <input type="text" name="comment" placeholder="post a comment" />
    </div>
  </div>
  {{/each}}
</script>
<div class="feed"></div>

P.S:我第一次遇到 (我已经读了几个小时了),所以可能有更好的方法

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27329040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档