如何通过Shopify商店中的handlebars.js循环使用JSON数组?

问题描述:

我正在向/cart.js发出GET请求。它返回以下JSON数据:如何通过Shopify商店中的handlebars.js循环使用JSON数组?

{ 
    "token":"118aa66ff7cc99c7fb4524f07bd305a4", 
    "note":null, 
    "attributes":{ 

    }, 
    "total_price":771, 
    "total_weight":0, 
    "item_count":3, 
    "items":[ 
     { 
     "id":903858951, 
     "title":"Aarts Frambozen op siroop", 
     "price":211, 
     "line_price":211, 
     "quantity":1, 
     "sku":"wi195688", 
     "grams":0, 
     "vendor":"Aarts", 
     "properties":null, 
     "product_id":385866167, 
     "variant_id":903858951, 
     "gift_card":false, 
     "url":"/products/aarts-frambozen-op-siroop?variant=903858951", 
     "image":"https://cdn.shopify.com/s/files/1/0656/8697/products/AHI_434d50303234343731_dRevLabel_1_Rendition_LowRes_JPG.jpeg?v=1413443204", 
     "handle":"aarts-frambozen-op-siroop", 
     "requires_shipping":true 
     }, 
     { 
     "id":903852739, 
     "title":"AH Aardappelschijfjes spek en ui", 
     "price":211, 
     "line_price":211, 
     "quantity":1, 
     "sku":"wi202676", 
     "grams":0, 
     "vendor":"AH", 
     "properties":null, 
     "product_id":385862935, 
     "variant_id":903852739, 
     "gift_card":false, 
     "url":"/products/ah-aardappelschijfjes-spek-en-ui?variant=903852739", 
     "image":"https://cdn.shopify.com/s/files/1/0656/8697/products/AHI_434d50303038363632_dRevLabel_2_Rendition_LowRes_JPG.jpeg?v=1413442904", 
     "handle":"ah-aardappelschijfjes-spek-en-ui", 
     "requires_shipping":true 
     }, 
     { 
     "id":903852571, 
     "title":"AH Aardappelen iets kruimig voordeelzak", 
     "price":349, 
     "line_price":349, 
     "quantity":1, 
     "sku":"wi127728", 
     "grams":0, 
     "vendor":"AH", 
     "properties":null, 
     "product_id":385862819, 
     "variant_id":903852571, 
     "gift_card":false, 
     "url":"/products/ah-aardappelen-iets-kruimig-voordeelzak?variant=903852571", 
     "image":"https://cdn.shopify.com/s/files/1/0656/8697/products/AHI_434d50303233343335_dRevLabel_1_Rendition_LowRes_JPG.jpeg?v=1413442897", 
     "handle":"ah-aardappelen-iets-kruimig-voordeelzak", 
     "requires_shipping":true 
     } 
    ], 
    "requires_shipping":true 
} 

我希望做的是迭代器在items并显示idtitlequantity

这里就是我想:

<script class="foobar" charset="utf-8" type="text/x-handlebars-template"> 
    <div> 
    {{#items}} 
     <thead> 
     <th>Id</th> 
     <th>Title</th> 
     <th>Qty</th> 
     </thead> 
     <tbody> 
     <tr> 
      <td>{{id}}</td> 
      <td>{{title}}</td> 
      <td>{{quantity}}</td> 
     </tr> 
     </tbody> 
    {{/items}} 
    </div> 
</script> 

<script charset="utf-8"> 
    var source = $(".foobar").html(); 
    var template = Handlebars.compile(source); 

    var jqxhr = $.getJSON("/cart.js", function() { 
    console.log("success"); 
    }) 

    $('body').append(template(jqxhr.responseJSON)); 
</script> 

但这返回:

Id Title Qty 

没有任何数据。我知道GET请求正在工作,因为console.log(jqxhr.responseJSON);会打印正确的数据。

我做错了什么,接下来我可以尝试什么?

编辑:

我想我错处理Ajax响应。

的jsfiddle:http://jsfiddle.net/narzero/4fn3f6sg

编辑2:

这可能是值得一提的是我运行一个Shopify商店代码。

+0

使用'{{#each项目}} {{id}} {{/每个}} – 2014-10-16 13:29:14

+0

http://jsfiddle.net/h2qyh8f2/这是您想要做的事情吗? – 2014-10-16 13:34:32

+0

@DennisMartinez这正是我想要的。但是当我用真正的JSON数据使用它时,它不起作用。我想我错误地处理了json响应。看看这个JSFiddle:http://jsfiddle.net/narzero/kgtLpe5g/ – narzero 2014-10-16 13:39:39

我通过添加标签{% raw %}{% endraw %}车把脚本类中,像这样解决了这个问题:

<script class="foobar" type="text/x-handlebars-template"> 
    {% raw %} 
    <div> 
     <thead> 
      <th>Id</th> 
      <th>Title</th> 
      <th>Qty</th> 
     </thead> 
     <tbody> 
     {{#items}} 
      <tr> 
      <td>{{id}}</td> 
      <td>{{title}}</td> 
      <td>{{quantity}}</td> 
      </tr> 
     </tbody> 
     {{/items}} 
    </div> 
    {% endraw %} 
</script> 

当使用{% raw %}它确保没有液体将这些标签内被解析。

感谢您的帮助球员。

你循环你提供的数据的方式应该可以工作,但是我发现ajax调用的一个小错误实际上会让你认为你的代码不工作。

由于您使用的是ajax,而且ajax是异步的,所以您必须在代码的完整回调中返回结果。

$.getJSON("/cart.js", function(data) { 
    $('body').append(template(data)); 
}); 

我不确定这是否有意故障,但是您的表是不完整的。

<script class="foobar" charset="utf-8" type="text/x-handlebars-template"> 
    <div> 
     <table> 
      <thead> 
       <th>Id</th> 
       <th>Title</th> 
       <th>Qty</th> 
      </thead> 
      <tbody> 
       {{#items}} 
       <tr> 
        <td>{{id}}</td> 
        <td>{{title}}</td> 
        <td>{{quantity}}</td> 
       </tr> 
       {{/items}} 
      </tbody> 
     </table> 
    </div> 
</script> 
+0

@narzero从我所看到的,ajax调用被注释掉了。它移动到外部脚本? – 2014-10-16 14:23:03

+0

不,我评论了一下其他的东西,对不起。你可以检查出来。所有关于主题的Ajax调用都在这里:http://cdn.shopify.com/s/files/1/0656/8697/t/7/assets/ajaxify.js?6701。你可能会在那里找到一些东西。例如,在线191上的Shopify.getCart .. – narzero 2014-10-16 14:27:14

+0

@narzero我不知道你是否正在利用这些方法(假设你的例子给出了),但使用方法getCart你可以做'Shopify.getCart(function(cart ){$('body')。append(template(cart));});' – 2014-10-16 14:45:57