バックボーンに不慣れで、Wordpress json プラグインから json を取得してテンプレートにレンダリングしようとしていますが、投稿をループすると、次のエラーUncaught ReferenceError: posts is not defined が表示されます。ありがとう...
jQuery(function($) {
var Post = Backbone.Model.extend();
var Posts = Backbone.Collection.extend({
model: Post,
url: '/api/get_post/?json=get_recent_posts',
parse: function(resp) {
console.log("posts", resp);
return resp;
}
});
var listView = Backbone.View.extend({
el: '#content',
template: _.template($("#post-template").html()),
initialize: function() {
this.model.bind("reset", this.render, this);
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
var AppRouter = Backbone.Router.extend({
routes: {
"!/archive": "archive"
},
archive: function() {
this.postList = new Posts();
this.postListView = new listView({
model: this.postList
});
this.postList.fetch();
this.postListView.render();
}
});
var app = new AppRouter();
Backbone.history.start();
});
テンプレート
<script id="post-template" type="text/template">
<ul>
<% _.each(posts, function(post) { %>
<li id="<%= post.id %>">
<a href="<%= post.url %>"><%= post.thumbnail %></a>
</li>
<% }); %>
</ul>
</script>
ジェイソン
{
"status": "ok",
"count": 1,
"count_total": 1,
"pages": 1,
"posts": [{
"id": 4,
"type": "post",
"slug": "test-post",
"url": "http:\/\/localhost:8888\/2013\/04\/test-post\/",
"status": "publish",
"title": "Test Post",
"title_plain": "Test Post",
"content": "",
"excerpt": "",
"date": "2013-04-17 15:12:21",
"modified": "2013-04-19 14:13:00",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "admin",
"url": "",
"description": ""
},
"comments": [],
"comment_count": 0,
"comment_status": "closed",
"thumbnail": "http:\/\/localhost:8888\/wp-content\/uploads\/2013\/04\/test-image-150x150.jpg"
}]
}