Backbone.Marionette的嵌套集合

Jai*_*era 9 backbone.js marionette

我想创建一个日历集合,每天都是约会的集合.日对象的结构是:

day:{
    date:'08-06-2012',
    appointment: {
        time_begin:'12:55',
        time_end: '16:40',
        customer: 'Jaime'
    }
}
Run Code Online (Sandbox Code Playgroud)

此刻我有这个模型和观点:

// CALENDAR COLLECTION
App.Calendar.Collection = Backbone.Collection.extend({
    // MODEL
    model: App.Day.Model
}
Run Code Online (Sandbox Code Playgroud)

当日历集合从服务器获取数据时,它将获得完整的日期对象,包括约会.

// 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())
});
Run Code Online (Sandbox Code Playgroud)

日期模型需要是约会的集合,并且不需要从服务器获取数据,因为它每天都在内部.

我怎样才能做到这一点?

Der*_*ley 15

如果我理解正确的问题,你问的是如何从你的Day模型,Appointment集合中获取数据到CalendarApointmentViewitemView?

Day.View可以设置为填充collection此复合视图,然后将其推入项目视图:


// DAY VIEW
App.Day.View = Backbone.Marionette.CompositeView.extend({
    collection: App.Day.Model,
    itemView: App.CalendarAppointment.View,
    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");

    }
});
Run Code Online (Sandbox Code Playgroud)

有一点需要注意:this.collection必须是有效的Backbone.Collection.如果模型将约会数据存储为简单数组,则需要执行以下操作:


var appointments = this.model.get("CalendarAppointments");
this.collection = new AppointmentCollection(appointments);

希望有所帮助.