嵌套的集合与Backbone.Marionette [英] Nested collections with Backbone.Marionette

查看:78
本文介绍了嵌套的集合与Backbone.Marionette的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a calendar which is a day collection, and every day is a collection of appointments. The structure of day object is:

day:{
    date:'08-06-2012',
    appointment: {
        time_begin:'12:55',
        time_end: '16:40',
        customer: 'Jaime'
    }
}

在这一刻我有这个模型和视图:

At this moment i have this models and views:

// CALENDAR COLLECTION
App.Calendar.Collection = Backbone.Collection.extend({
    // MODEL
    model: App.Day.Model
}

在日历采集获取数据形成服务器,它得到完整的一天对象,包括约会。

When calendar collection fetch data form the server, it gets complete day objects including appointments.

// 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())
});

天模型需要预约的集合,因为它的内每天应没有必要从服务器获取的内容

The day model needs to be a collection of appointments and shouldn't be necessary to fetch data from the server because it's inside every day.

我怎样才能做到这一点?

How can i do this?

推荐答案

如果我明白这个问题吧,你问如何从模型获取数据,预约收集,到 CalendarApointmentView ItemView控件?

if I understand the question right, you're asking how to get the data from your Day model, Appointment collection, in to the CalendarApointmentView itemView?

Day.View 可设置来填充此复合视图,那么这将是在推进该项目的意见:

Your Day.View can be set up to populate the collection of this composite view, which will then be pushed in to the item views:


// 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 必须是有效的Backbone.Collection。如果你的模型存储任命数据作为一个简单的数组,那么你需要做的是:

One thing to note: the this.collection must be a valid Backbone.Collection. If your model stores the appointment data as a simple array, then you need to do this:


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

希望有所帮助。

这篇关于嵌套的集合与Backbone.Marionette的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆