1
votes

I have a calendar day model, and in each calendar day, I have a collection of events.

If the day has events, the collection is refreshed, no problem, I show the new events. However, if the day has no events, the collection isn't being emptied, and the collection still shows the events from the day which was the last model (previous day if the user went to next day).

I've got the collection being created in the fetch success of the model, so it shouldn't be an async issue.

My model is


Myapp.Models.CalDay = Backbone.Model.extend({
    url:'calendar',
    initialize: function(){

        this.get_cal();
        },
    get_cal: function(){

        //calendar doesn't exist, so first time through,
            Myapp.CurrentCal = this;
            Myapp.cal.set({'date': new Date(),'draw_slider': true,'put_date':'today'});
            Myapp.CurrentCal.Events = new Myapp.Collections.DayEvents();
            this.bind('change:date',this.fetch_cal);
            this.fetch_cal();
        } 

        Myapp.CurrentCal.bind("fetched",this.get_view);
    },

    fetch_cal: function(){

        console.log(Myapp.CurrentCal.Events);

        Myapp.Calendar.fetch({
        success: function(response) {

            Myapp.CurrentCal.Events.reset(response.attributes.calendar_events);
            Myapp.CurrentCal.trigger("fetched");
                },
          error: function() {
              alert('error getting calendar');
            }
        });


    },
    get_view: function(){
          console.log(Myapp.CurrentCal.Events);
          new Myapp.Views.CalendarDay();

    }
});
2
Are you calling remove() on the view when a new day is accessed? - rudolph9
Ok, went ahead and answered with what I think the issue likely is. If I'm wrong, will you post your view and controller definitions too? - rudolph9
sorry @rudolph9 I wasn't expecting an answer that quickly. I've responded below to your answer. - pedalpete
if the day has no events what does response.attributes.calendar_events return ? And does it go in success callback ? - Cyclone
@Cyclone the callback does not return a calendar_events array when there are no events. I does trigger the response success. I was checking for response.attributes.calendar_events.length, and then reset, but it was alway returning as having the length of the old calendar day when there were events. So, same issue. Checking for response.attributes.calendar_events.length>0 or response.attributes.calendar_eventsis always triggered if it has a calendar_events or not. - pedalpete

2 Answers

0
votes

As documentation says calling reset without passing any models as argument will empty collection. So for all the days where there are no events you need to call reset without passing models as an argument or return empty array from the server as you said.

And / Or you can make a check for response in the success callback and based on the conditions call either

Myapp.CurrentCal.Events.reset(response.attributes.calendar_events);

or

Myapp.CurrentCal.Events.reset();
0
votes

You need to call remove() your view before (well, I'd sugest before) rendering the next days events.

This in the case where you render a new days with events this is not causing you issues because the container of the view is being over written with these events. However, in the case where the next day contains zero new events, nothing is written and hence you are left with the previous days events still being displayed.

Consider creating a close method for your view and along with executing the remove() function on the view also clean up any binding you have associated with that view.

I have been creating a config/backone.coffee for my backbone apps and creating a close() function to accomplish this:

class Backbone.View extends Backbone.View
  close: ->
    @beforeClose() if @beforeClose?
    @remove()
    @unbind(null,null,this)