Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 带主干的嵌套集合。木偶_Backbone.js_Marionette - Fatal编程技术网

Backbone.js 带主干的嵌套集合。木偶

Backbone.js 带主干的嵌套集合。木偶,backbone.js,marionette,Backbone.js,Marionette,我想创建一个日历,它是一个日期集合,并且每天都是一个约会集合。day对象的结构是: day:{ date:'08-06-2012', appointment: { time_begin:'12:55', time_end: '16:40', customer: 'Jaime' } } 目前,我有以下模型和视图: // CALENDAR COLLECTION App.Calendar.Collection = Backbon

我想创建一个日历,它是一个日期集合,并且每天都是一个约会集合。day对象的结构是:

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模型需要是约会的集合,不需要从服务器获取数据,因为它每天都在服务器中


如何才能做到这一点?

这似乎是一个完美的用例,它将自动解析嵌套数据并创建嵌套集合结构。

如果我理解正确,您会问如何从您的
模型,
约会
集合中获取数据,是否进入
CalendarApointmentView
itemView

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


// 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");

    }
});
需要注意的是:
this.collection
必须是有效的主干.collection。如果您的模型将约会数据存储为简单数组,则需要执行以下操作:


var Appoints=this.model.get(“CalendarAppoints”);
this.collection=新约会集合(约会);


希望这能有所帮助。

问题是否更多地围绕着如何构造视图?@Derick,对于嵌套较深的集合,compositeView.itemView能否指向另一个compositeView?你能把它们埋得多深有限制吗?

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