Question

I've got a backbone.js model which contains a calendar. The user can go back and forth in the calendar and I can get the events for the selected calendar day.

In my Model, I have

initialize: function(){
    this.on('change:date',this.get_cal());
},
get_cal: function(){
    alert('get calendar');
    this.fetch(...
}

and in my view I have

 cal_date: function(move){
    Myapp.cal.attributes.date.setDate(Myapp.cal.attributes.date.getDate()+move);

}

when the date changes, I expected backbone to trigger the change event, and get the calendar events for the new date. Unfortunately, that isn't happening.

I've also tried putting the printed date into the model as

Myapp.cal.set({print_date: formatted_date});

thinking that maybe backbone is missing the update because I'm not calling 'set', or because it sees a date object and thinks that it already had a date object and therefore didn't change.

I've also tried to trigger the change with Myapp.cal.trigger('change'), in the view but that didn't work either. Nor did removing the calendar events by calling Myapp.cal.cal_events.refresh() where cal_events is the collection holding the days events.

Do you see what's wrong here?

Was it helpful?

Solution

I think the issue is with the parentheses after get_call on this line:

this.on('change:date',this.get_cal());

You should remove them because they call get_call right in initialize instead of making them an event handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top