handlebarsjs是否与JSONObject一起使用?我有一个数组的对象来自php中的db,它总是一个std对象。
PHP中,
$items = std object
echo json_encode($items);
结果,
{"items":{"0":{"pub_name":"Main","system_id":"50","make_accessible":"0","count":"19","router":"#!page\/main\/list\/"},"1":{"pub_name":"estate","system_id":"122","make_accessible":"0","count":"8","router":null},"2":{"pub_name":"wines","system_id":"125","make_accessible":"0","count":"5","router":null},"3":{"pub_name":"visits","system_id":"128","make_accessible":"0","count":"4","router":null},"4":{"pub_name":"community","system_id":"131","make_accessible":"0","count":"8","router":null},"5":{"pub_name":"events","system_id":"137","make_accessible":"0","count":"8","router":null},"6":{"pub_name":"Contact","system_id":"140","make_accessible":"0","count":"3","router":null},"7":{"pub_name":"Newsletter","system_id":"143","make_accessible":"0","count":"2","router":null},"8":{"pub_name":"gallery","system_id":"146","make_accessible":"0","count":"2","router":null},"9":{"pub_name":"Discover Our Wines","system_id":"163","make_accessible":"0","count":"2","router":null}},"total":10}
jquery的,
$.ajax({
type: "GET",
dataType: "json",
url: "server.php"
}).done(function(returndata) {
$(this).html(Handlebars.getTemplate('page')({pages: returndata}));
...
returndata,
Object { items={...}, total=10}
把手的模板,
{{ pages.items.length }}
结果,
empty
我应该怎么做,因此把手模板可以读取JSONObjects?
答案 0 :(得分:1)
对象没有长度属性,因此Handlebars无需渲染。
var myObject = {items: {0: {k: 1}, 1: {k: 2}}};
var myArray = [{k: 1}, {k: 2}];
alert(myObject.items.length); // undefined
alert(myArray.length); // 2
我建议你调整返回JSON的代码,这样items
就是一个数组。
或者,你需要这样的东西:Length of a JavaScript object