Я изучаю разработку JavaScript MVC-приложений с использованием Backbone.js и проблемы с отображением коллекции моделей в представлении. Вот что я хочу сделать:
-
После завершения загрузки страницы извлекает данные с сервера в виде коллекции моделей
-
Отобразить их в представлении
Это все, что я хочу сделать, и вот что я до сих пор:
$(function(){
"use strict";
var PostModel = Backbone.Model.extend({});
var PostCollection = Backbone.Collection.extend({
model: PostModel,
url: 'post_action.php'
});
var PostView = Backbone.View.extend({
el: "#posts-editor",
initialize: function(){
this.template = _.template($("#ptpl").html());
this.collection.fetch({data:{fetch:true, type:"post", page:1}});
this.collection.bind('reset', this.render, this);
},
render: function(){
var renderedContent = this.collection.toJSON();
console.log(renderedContent);
$(this.el).html(renderedContent);
return this;
}
});
var postList = new PostCollection();
postList.reset();
var postView = new PostView({
collection: postList
});
});
Проблема
Насколько я знаю, Chrome регистрирует ответ с сервера, и он в формате JSON, как я хочу. Но на мой взгляд это не делает. В консоли нет видимых ошибок.
На сервере есть обработчик, который принимает параметры GET и эхо некоторых JSON:
http://localhost/blog/post_action.php?fetch=true&type=post&page=1
[
{
"username":"admin",
"id":"2",
"title":"Second",
"commentable":"0",
"body":"This is the second post."
},
{
"username":"admin",
"id":"1",
"title":"Welcome!",
"commentable":"1",
"body":"Hello there! welcome to my blog."
}
]