0

What's happening to controller when we're quitting the appropriate route? Is that correct that observers set up there keep doing their job? And if so, what is the proper way to avoiding that? Some method opposite to setupController?

Ivan Yurov
  • 1,578
  • 10
  • 27

1 Answers1

1

yes, observers are still present, what I normally do with observers observing another property that could change in another screen, is that I set them/remove them manually in the activate/deactivate route's hooks, something like this:

var controllerWhereThePropertyToObserveIs = this.controllerFor('fancyController');
controllerWhereThePropertyToObserveIs.addObserver('propertyToObserveForChanges', this.controllerFor('controllerWhereTheObserverWouldBe'), 'functionToFire');

then, to remove it:

var controllerWhereThePropertyToObserveIs = this.controllerFor('fancyController');
controllerWhereThePropertyToObserveIs.removeObserver('propertyToObserveForChanges', this.controllerFor('controllerWhereTheObserverWouldBe'), 'functionToFire');
fanta
  • 1,489
  • 13
  • 15
  • Hm... It should work, but looks like a hack. There must be better solution. The problem is that Ember's MVC is not really MVC in its common meaning. I just checked and found that controller is for decorating model and not the route or the view as anyone could expect (having some experience with any MVC web-framework). – Ivan Yurov Nov 01 '13 at 08:54
  • Yes, you're right, Ember's controllers are for decorating models, not the view nor the route. However, that's the only way I've found to tackle that issue. If you find another solution, please post it here. – fanta Nov 01 '13 at 15:08
  • I don't get one thing. Why they delete the context on exit from route, but keep the controller alive. (https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/vendor/router.js row 776) Why not to keep it as long as controller is alive or vice versa — why not to kill both. – Ivan Yurov Nov 01 '13 at 16:38
  • Tried your solution. And what's weird, it made it out to fire one last time before quitting the route. And in that moment there already is no value in the variable I observe. May be willTransition hook will work before destroying the context... But this all still ugly :( Never felt such love&hate mix about one particular tool :) – Ivan Yurov Nov 01 '13 at 21:45
  • That's weird, I've used this technique many times, and I haven't had any problems with it – fanta Nov 05 '13 at 15:09
  • Well, now it's working. I used willTransition hook instead of deactivate. And I still have no better solution, though I switched to the other tasks. – Ivan Yurov Nov 05 '13 at 19:22