jquery - How look up JSON object in Handlebar expressions -
i'm using handlebars , trying use post's userid find user's username , pic; i'm using userid placeholder correct expression. i've tried several ways data json files instead of having inline, haven't gotten right.
{{#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 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 happen know capone is? " } ] }, { "id": 2, "userid": 2, "content":"day 2 of house sitting...awww firends 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));
if convert users array object keys userid
's like:
users: { "1": { "username": "james bond", "pic": "http://i49.tinypic.com/28vepvr.jpg", }, "2": { "username": "william forrester", "pic": "http://i46.tinypic.com/2epim8j.jpg", } }
then can create 2 custom helpers shown below:
var data = { posts: [{ "id": 1, "userid": 1, "content": "love wine? love food?", "comments": [{ "id": 13, "postid": 1, "userid": 2, "content": "would happen know capone is? " }] }, { "id": 2, "userid": 2, "content": "day 2 of house sitting...awww firends 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));
.post{ margin:5px; } .post-comments{ padding-left:20px; } img { width: 50px; }
<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 comment" /> </div> </div> {{/each}} </script> <div class="feed"></div>
or can keep array , iterate on until find match inside helpers, costly:
var data = { posts: [{ "id": 1, "userid": 1, "content": "love wine? love food?", "comments": [{ "id": 13, "postid": 1, "userid": 2, "content": "would happen know capone is? " }] }, { "id": 2, "userid": 2, "content": "day 2 of house sitting...awww firends 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));
.post { margin: 5px; } .post-comments { padding-left: 20px; } img { width: 50px; }
<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 comment" /> </div> </div> {{/each}} </script> <div class="feed"></div>
p.s: first handlebars encounter (i've been reading this past few hours) there might better way
Comments
Post a Comment