0
我在创建多个集合和模型时遇到了问题。我正在尝试构建一个基本的Backbone.js集合。在Backbone.js中形成多个集合和模型的问题
我有两个问题。
- 望着通过什么传递给视图我可以看到实际上没有被创建的
Event
模型(没有ID:在属性“无ID”)的控制台日志。我知道收集月份形成月份模型,但我无法解释为什么事件不会形成事件模型。 - 集合仅在Months集合初始化为reset:true时形成。我不知道如何解释这
我有一个Months
的模型Month
的集合。
{
"status": "success",
"year": 2014,
"months": [
{
"month": 1,
"events": [
{
"title": "None",
"startdate": "2014-01-23",
"enddate": "None",
"description": "description 1"
}
]
},
{
"month": 12,
"events": [
{
"title": "None",
"startdate": "2014-12-24",
"enddate": "None",
"description": "description 2"
},
{
"title": "None",
"startdate": "2014-12-25",
"enddate": "None",
"description": "description 3"
}
]
}
]
}
任何帮助,将不胜感激
:
$(function(){
var Event = Backbone.Model.extend({
defaults: {
id: 'No ID',
description: 'No description',
startdate: 'No startdate'
}
});
var Events = Backbone.Collection.extend({
model: Event,
parse: function(response) {
return response;
}
});
var Month = Backbone.Model.extend({
defaults: function() {
return {
events: new Events(this.get('events'))
};
}
});
var Months = Backbone.Collection.extend({
model: Month,
url : '/api/v1/calendar/2014',
initialize: function(){
this.fetch({
reset: true
});
},
parse: function(response) {
if ('months' in response) {
console.log('Months Collection: ' + response);
return response.months;
} else {
console.error('No months found.');
}
}
});
window.Calendar = new Months();
});
的JSON我想在这里解释的例子:Month
有模型的Events
集合Event
如下模型谢谢