0

I've got a backbone calendar, and each day has a bunch of options. The user only sees one day at a time.

So when the user selects to go to the next or previous day, I call the MyApp.Views.ShowDay to redraw the day.

the ShowDay view calls a few sub-views MyApp.Views.Hours. I need to unbind the hours from the old day when I create a new day, but that is causing nothing but errors.

The way I'm trying to do it is

MyApp.Views.Showday = Backbone.View.extend({
    events: {
             "click div.change_day":"change_day"
    },

    change_day: function(){
      MyApp.Views.Hours.unbind(); //I've tried off(), off, remove() as well

    }

});

Is this not the way to unbind children?

pedalpete
  • 21,076
  • 45
  • 128
  • 239

2 Answers2

1

You need to explicitly unbind the event handlers from the event. See this answer to another SO question, paying specific attention to the bindTo and unbindFromAll functions:

Backbone.js : repopulate or recreate the view?

Community
  • 1
  • 1
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
  • thanks Derick, I had actually read your article on Los Techies regarding your binding technique, but i was hoping for something simpler. I'm giving this a try now. – pedalpete Mar 08 '12 at 19:41
  • unfortunately, there isn't anything simpler. Backbone takes a hands-off approach to this, and lets us either shoot ourselves in the foot or write the extra code ourselves. FWIW: https://github.com/derickbailey/backbone.marionette/blob/master/backbone.marionette.js#L463-488 just copy & paste the code from there and use _.extend to mix it in to your own stuff. :) – Derick Bailey Mar 08 '12 at 19:47
0

Try MyApp.Views.Hours.remove(). This removes the view and its elements from the dom and tears down any event handlers in the process. Then recreate the hours view

Typo Johnson
  • 5,974
  • 6
  • 29
  • 40
  • it could be because of the way I wrote my nesting, but this also didn't allow me to add another child after removing it. I had tried that earlier. – pedalpete Mar 08 '12 at 23:47