最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Variable is undefined inside 'each' in Handlebars.js - Stack Overflow

programmeradmin1浏览0评论

I have my render function:

renderTracksList = function(tracks){
    var source   = $("#timeline-template").html();
    var template = Handlebarspile(source);
    var data     = {
                    tracks: tracks, 
                    client_id: App.config.client_id
                };

    var html     = template(data);

    $('#timeline').html(html);
}

And in my template I'm trying to use print client_id inside the loop, but in the loop context it's undefined, so how to access and print the variable?

{{#each tracks}}
<div class="track">
    {{title}} - 
    <a href="javascript:App.play('{{permalink_url}}')">PLAY</a>
    {{#if downloadable}}
    - <a href="{{download_url}}?client_id={{client_id}}" target="_blank">DOWNLOAD</a>
    {{/if}}
</div>
{{/each}}

Btw, I've already tried this:

<a href="{{download_url}}?client_id={{../client_id}}" target="_blank">DOWNLOAD</a>

I have my render function:

renderTracksList = function(tracks){
    var source   = $("#timeline-template").html();
    var template = Handlebars.pile(source);
    var data     = {
                    tracks: tracks, 
                    client_id: App.config.client_id
                };

    var html     = template(data);

    $('#timeline').html(html);
}

And in my template I'm trying to use print client_id inside the loop, but in the loop context it's undefined, so how to access and print the variable?

{{#each tracks}}
<div class="track">
    {{title}} - 
    <a href="javascript:App.play('{{permalink_url}}')">PLAY</a>
    {{#if downloadable}}
    - <a href="{{download_url}}?client_id={{client_id}}" target="_blank">DOWNLOAD</a>
    {{/if}}
</div>
{{/each}}

Btw, I've already tried this:

<a href="{{download_url}}?client_id={{../client_id}}" target="_blank">DOWNLOAD</a>
Share Improve this question edited Nov 2, 2013 at 23:51 Diosney 10.6k15 gold badges68 silver badges113 bronze badges asked Apr 1, 2013 at 20:36 Rafael Santos SáRafael Santos Sá 191 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Try to repeat ../ for both the {{#if}} and the {{#each}}:

<a href="{{download_url}}?client_id={{../../client_id}}" target="_blank">DOWNLOAD</a>

Even though {{#if}} keeps the value of the previous context, it still creates another entry in the stack that ../ steps back through.

发布评论

评论列表(0)

  1. 暂无评论