How to reset controller properties on exit


#1

I would like to reset all the properties to null for a given controller everytime I use it.

App.UsersCreateController = Ember.ObjectController.extend({
userName: null,
userEmail: null,

actions: {
    save: function(){
        var name = this.get('userName'),
            email = this.get('userEmail');
        ...

        // Clear out the values for next time
        this.set('userName', null);
        this.set('userEmail', null);

        this.transitionToRoute('users');
    }
});

Unfortunately this does not seem to work, as the next time I fire up the edit user dialog the previous values still appear in the form.


#2

Perhaps using the route’s willTransition action is what you need here.

From the docs:

The willTransition action is fired at the beginning of any attempted transition with a Transition object as the sole argument. This action can be used for aborting, redirecting, or decorating the transition from the currently active routes.

      App.IndexRoute = Ember.Route.extend({
        queryParams: {
          query: {
          refreshModel: true
          }
        },
        actions: {
          willTransition: function(transition) {
            this.controller.set('queryField').clear();
          }
        },
        model: function(params) {
          if (!params.query) {
            return []; // no results;
          }
          return Ember.$.getJSON('http://api.soundcloud.com/tracks', {client_id: 'YOUR_CLIENT_ID', limit: 25, q: params.query });
        }
      });

I just did the above to clear a queryField on transition.

You could probably do the same with the didTransition action.


#3

The resetController hook was added in 1.7 along with query parameters.

resetController: function(controller, isExiting, transition) {
    this._super.apply(this, arguments);

    if (isExiting) {
        controller.resetData(); // or whatever function you want to call
    }
}

#4

Thanks, I didn’t know about the resetController hook!


#5

Thanks, but when I tried this it didn’t work.

The resetController method is NOT being called

Upon closer inspection I see that this is because my route is only changing from /users/create to /users which might not be sufficient to trigger this event.


#6

I’m having this same problem; neither resetController nor willTransition fire when reloading the route, or when loading a new model to the route (e.g. going from posts/12345/comments to posts/98765/comments). I have a controller property that’s keeping its value through transitions, and I need specifically clear it for some reason every time, whether it’s a refresh or a different model.


#7

Controllers are singletons. Needing to “reset” them indicates a code smell. It means you’re trying to use the controller as a transient, dynamic, per-visit object. What are userName and userEmail, where do they come from, what do they relate to? Unless they are a persistent properties of the controller each time it is visited, they should be being held elsewhere. If they are already on the model, then refer to them via the model. if they are related to some user display, then make them part of a component, which will be re-instantiated and torn down every time the route is visited.