使用 Backbone.Marionette 嵌套集合 [英] Nested collections with Backbone.Marionette

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

问题描述

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

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.

我该怎么做?

推荐答案

如果我理解正确,您是在问如何从 Day 模型中获取数据,Appointment 集合,放入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 可以设置为填充此复合视图的 collection,然后将其推送到项目视图中:

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天全站免登陆