我用json对来自数据库的响应进行了编码,得到了一个具有这种结构的json,我从带有json_encode的控制器(我用php工作)发送
了回显:[
{column1: value, column2: value...} //row 1
{column1: value, column2: value...} //row 2
.... //row n
]
我的脚本(我在车把中的模板)是:
<script id="handlebars_deals_list" type="text/x-handlebars-template">
{{#each data}}
{{tittle}}
{{/each}}
</script>
我以这种方式传递上下文:
var source=jQuery('#handlebars_deals_list').html();
var template = Handlebars.compile(source);
var context={data:response};//response is the json data I showed earlier
console.log(context); //Object{data="'n[{"id":"149417","biz_n......null,"index_deal":"0"}]"}
var html=template(context);
console.log(html); //empty!!?!?!?!!?!?!?!?!, why????
但是我没有看到任何模板呈现
在与这个问题进行了一些斗争之后,我有了解决方案:
function process_deals_date(response){
var response=jQuery.parseJSON(response); //THIS IS THE KEY
var source=jQuery('#my_template').html();
var template = Handlebars.compile(source);
Var html=template(response);
console.log(html); //RIGHT POPULATED THE TEMPLATE
}