我想创建一个日历集合,每天都是一个约会的集合.日对象的结构是:
day:{ date:'08-06-2012',appointment: { time_begin:'12:55',time_end: '16:40',customer: 'Jaime' } }
在这一刻,我有这个模型和意见:
// CALENDAR COLLECTION App.Calendar.Collection = Backbone.Collection.extend({ // MODEL model: App.Day.Model }
当日历收集从服务器获取数据时,它将获得包括约会在内的完整的日期对象.
// CALENDAR VIEW App.Calendar.View = Backbone.Marionette.CompositeView.extend({ // TEMPLATE template: Handlebars.compile(templates.find('#calendar-template').html()),// ITEM VIEW itemView: App.Day.View,// ITEM VIEW CONTAINER itemViewContainer: '#calendar-collection-block' }); // DAY MODEL App.Day.Model = Backbone.Collection.extend({ // PARSE parse:function(data){ console.log(data); return data; } }); // DAY VIEW App.Day.View = Backbone.Marionette.CompositeView.extend({ collection: App.Day.Model,itemView: App.CalendarAppointment.View,//---->I NEED TO DEFINE THIS,NOT SURE HOW template: Handlebars.compile(templates.find('#day-template').html()) });
日间模型需要是约会的集合,并且不需要从服务器获取数据,因为它在每一天.
我该怎么办?
解决方法
如果我明白这个问题,你会问如何从Day Day模型中获取数据,预约收集到CalendarApointmentView itemView?
您的Day.View可以设置为填充此复合视图的集合,然后将其推入项目视图:
// DAY VIEW App.Day.View = Backbone.Marionette.CompositeView.extend({ collection: App.Day.Model,template: Handlebars.compile(templates.find('#day-template').html()),initialize: function(){ // since `this.model` is a `Day` model,it has the data you need this.collection = this.model.get("CalendarAppointments"); } });
有一件事要注意:this.collection必须是一个有效的Backbone.Collection.如果您的模型将约会数据存储为简单数组,则需要执行以下操作:
var appointments = this.model.get("CalendarAppointments"); this.collection = new AppointmentCollection(appointments);
希望有帮助.