2012-01-13 50 views
0

我試圖枚舉並構建以下撥弄Mustache.js:這裏未能枚舉部分中的對象與JSONP和小鬍子的JavaScript模板

$(function() { 
    var choices = { "users": [ 
     { "first_name": "Ryan", 
      "last_name": "Pays", 
      "pic_square": "/Global/profile/thumb/placeholder.jpg", 
      "product_name": "Merlin - the complete box set", 
      "product_picture": "/Global/products/full/box-set.jpg" 
     }, 
     { "first_name": "Eric", 
      "last_name": "Li Koo", 
      "pic_square": "/Global/profile/thumb/placeholder.jpg", 
      "product_name": "Merlin - Series 4 volume 1", 
      "product_picture": "/Global/products/full/box-set.jpg" 
     }, 
     { "first_name": "Abdul", 
      "last_name": "Raouf", 
      "pic_square": "/Global/profile/thumb/placeholder.jpg",  
      "product_name": "Merlin - the complete box set", 
      "product_picture": "/Global/products/full/box-set.jpg" 
     }] 
    }; 
    $.getJSON("http://jsfiddle.net/echo/jsonp/?callback=?", choices, function (data) { 
     console.log(data); 
     var template = "<ul>{{#users}}" + 
         "<li>" + 
      "<p><strong>{{first_name}} {{last_name}}</strong> likes {{product_name}}</p>" + 
         "</li>" + 
         "{{/users}}</ul>", 
      html = Mustache.to_html(template, data); 
     $('.wrapper').html(html); 
    }); 
}); 

示例 - >http://jsfiddle.net/mhMJA/3/

它正確地記錄對控制檯的JSON響應,但似乎沒有建立模板。如果我只是將單個用戶傳遞給JSONP回調,它可以正常工作。

在此先感謝。

+0

將您的代碼添加到問題 - jsfiddle是一個很棒的網站,但有時非常慢 - 它通常更好地包含您的代碼和jsfiddle示例..... – ManseUK 2012-01-13 12:00:24

回答

2

這不是你的錯。這是jsfiddle可以幫助您將您的JSON對象轉換爲以下格式。

{"users[0][first_name]":"Ryan","users[1][first_name]":"Eric","users[2][first_name]":"Abdul","users[2][product_picture]":"/Global/products/full/box-set.jpg","users[1][product_picture]":"/Global/products/full/box-set.jpg","users[1][pic_square]":"/Global/profile/thumb/placeholder.jpg","users[1][product_name]":"Merlin - Series 4 volume 1","users[0][last_name]":"Pays","users[0][product_picture]":"/Global/products/full/box-set.jpg","users[1][last_name]":"Li Koo","users[0][product_name]":"Merlin - the complete box set","users[0][pic_square]":"/Global/profile/thumb/placeholder.jpg","users[2][last_name]":"Raouf","users[2][pic_square]":"/Global/profile/thumb/placeholder.jpg","users[2][product_name]":"Merlin - the complete box set","_":"1326530878282"} 

我建議你編寫自己的JSONP應用程序來避免這個問題。 Here是我用JSON2來獲取JSON對象。