Nested collections with Backbone.Marionette

前端 未结 2 1324
不知归路
不知归路 2020-12-14 05:06

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-         


        
相关标签:
2条回答
  • 2020-12-14 05:33

    This looks like the perfect usecase for backbone-relational, which would automatically take care of parsing your nested data and creating the nested collection structure.

    0 讨论(0)
  • 2020-12-14 05:35

    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?

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

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

    Hope that helps.

    0 讨论(0)
提交回复
热议问题