列表中呈现的骨干嵌套json对象
Backbone nested json object rendered in list
我目前正在尝试在ul中呈现这个json对象。我希望能够循环浏览GamesList,并在列表中获得游戏及其属性。我有点碰壁了,我不完全确定如何做到这一点。对骨干来说还是很新的,所以任何帮助都将不胜感激。
JSON对象:
{
"GamesList":[
{
"Date":"2013/07/02",
"Games":[
{
"Id":"3252",
"Time":"12:10 AM"
}
]
},
{
"Date":"2013/07/02",
"Games":[
{
"Id":"3252",
"Time":"12:10 AM"
}
]
},
{
"Date":"2013/07/02",
"Games":[
{
"Id":"3252",
"Time":"12:10 AM"
}
]
}
]
}
应用程序结构:
App.Models.Game = Backbone.Model.extend({
defaults: {
GamesList: ''
}
});
App.Collections.Game = Backbone.Collection.extend({
model: App.Models.Game,
url: 'path/to/json',
parse: function (response) {
return response;
}
});
App.Views.Games = Backbone.View.extend({
tagName: 'ul',
initialize: function () {
this.collection = new App.Collections.Game();
this.listenTo(this.collection, 'reset', this.render, this);
this.collection.fetch();
},
render: function () {
//filter through all items in a collection
this.collection.each(function (game) {
var gameView = new App.Views.Game({
model: game
});
this.$el.append(gameView.render().el);
}, this)
return this;
}
});
App.Views.Game = Backbone.View.extend({
tagName: 'li',
template: _.template($('#gameTemplate').html()),
render: function () {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var gameCollection = new App.Collections.Game();
gameCollection.fetch({
data: {
collection_id: 25
},
success: function (data, textStatus, jqXHR) {
console.log(data);
console.log(textStatus);
console.log(jqXHR);
console.log('success');
},
error: function () {
alert('Oh noes! Something went wrong!')
}
});
var gamesView = new App.Views.Games({
collection: gameCollection
});
$(document.body).append(gamesView.render().el);
看起来您的JSON对象没有与Backbone.Collection内联…
正如您所声明的,App.Collections.Game
具有url /path/to/json
,这意味着需要返回的json是一个列表。。。没有JSON 中显示的GamesList
编辑:
您可以使用Games Collection中的解析函数来修复从服务器中检索到的json
parse:function(response){
return response.GamesList;
}
重要:请注意,从服务器获取的json对象应该有ID。Backbone会"认为"这些模型是新的,并在保存时创建它们。。。
我看到它有点混乱。让我们一步一步地进行:
---------评论后--------
您可以将您的模型设置为:
defaults: {
Date:'',
Games:''
}
然后将解析函数修改为
parse: function (response)
{
var _this = this;
_.map(response, function(obj) {
_this.add(obj)
});
}
通过这种方式,您可以根据模型的期望添加集合中的每一项。
我看到的另一个问题是,您要创建和获取集合两次:
...
this.collection = new App.Collections.Game();
this.listenTo(this.collection, 'reset', this.render, this);
this.collection.fetch();
...
然后
var gameCollection = new App.Collections.Game();
...
gameCollection.fetch({
data: {
....
...
var gamesView = new App.Views.Games({
collection: gameCollection
});
相关文章:
- 访问嵌套JSON对象的键,其中键是动态的
- 从多维嵌套json数组创建下拉列表
- 带嵌套json的下划线js查找
- 返回嵌套JSON中包含特定键的所有值
- 嵌套JSON到平面HTML表
- 原型Ajax请求参数为嵌套json
- 从对象数组中动态创建嵌套json
- ExtJS 4.1-检索嵌套JSON的hasOne信息
- 更新嵌套json Angularjs中的对象
- 访问嵌套 json 对象的属性将返回未定义
- 如何解析没有键的嵌套json's的javascript
- 显示嵌套json集合的主干
- 列表中呈现的骨干嵌套json对象
- 如何使用promise来反规范化嵌套json
- 正在读取嵌套json,jquery返回undefined
- Ionic/Angular:如何在嵌套json中导航
- 如何访问AngularJs中的嵌套Json对象
- 使用Javascript查找并更新嵌套JSON数组和对象中的所有键值
- 如何获取给定表单详细信息的嵌套json对象
- 通过Javascript中的递归迭代映射嵌套JSON