首先获取信息,这是我对服务器的回复
$response = array();
$i = 0;
while($result = mysql_fetch_assoc($query)){
$response[$i]["id"] = $result["ID"];
$response[$i]["post_date"] = $result["post_date"];
$response[$i]["post_content"] = $result["post_content"];
$response[$i]["nickname"] = $result["nickname"];
$i++;
}
echo json_encode($response);
似乎@ k33g_org方式确实有效但现在控制台说我Uncaught ReferenceError: nickname is not defined
我的模板是
<script type="text/template" id="tplhome_post_list">
<div class="post">
<div class="view">
<p class="header_post">
<%= nickname %> - Le <%= post_date %>
</p>
<div class="statut">
<div class="like">
8
</div>
<div class="reply">
25
</div>
</div>
<p class="clear">
<%= post_content %>
</p>
</div>
</div>
</script>
如何解决?定义模板值??
答案 0 :(得分:0)
你必须写这样的抓取:
Posts.fetch({
success:function(data) {
/* when you get data from server request data variable is populated */
/* then you can call render() method of the view */
},
error : function(err) {
throw "Houston we've got a problem...";
}
});
您可以将数据传递给渲染方法(类似这样)
render : function(data) {
var renderedContent = this.template(this.data.toJSON());
$(this.el).html(renderedContent);
return this;
}
或者,更好的是,您可以将集合的重置事件绑定到视图,在视图的initialize
方法中,将其放入:
_.bindAll(this, 'render');
this.collection.bind('change', this.render);
this.collection.bind('add', this.render);
this.collection.bind('remove', this.render);
然后当收集内容发生变化时(即提取时),render()
被称为