# How to reset controller properties on exit

**URL:** <https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664>\
**Category:** Uncategorized\
**Created:** [November 11, 2014, 2:17pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664 "2014-11-11T14:17:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgish](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kgish/32/13333_2.png) [@kgish](https://discuss.emberjs.com/u/kgish)\
**Post date:** [November 11, 2014, 2:17pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664/1 "2014-11-11T14:17:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimnanney](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jimnanney/32/15967_2.png) [@jimnanney](https://discuss.emberjs.com/u/jimnanney)\
**Post date:** [November 11, 2014, 2:39pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664/2 "2014-11-11T14:39:40Z")

</div>

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.

```auto
      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.

---

<div class="post-metadata">

**Author:** ![gordon\_kristan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/gordon_kristan/32/14862_2.png) [@gordon\_kristan](https://discuss.emberjs.com/u/gordon_kristan)\
**Post date:** [November 11, 2014, 3:07pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664/3 "2014-11-11T15:07:30Z")

</div>

The [resetController](http://emberjs.com/api/classes/Ember.Route.html#method_resetController) hook was added in 1.7 along with query parameters.

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

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

```

---

<div class="post-metadata">

**Author:** ![jimnanney](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jimnanney/32/15967_2.png) [@jimnanney](https://discuss.emberjs.com/u/jimnanney)\
**Post date:** [November 11, 2014, 3:16pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664/4 "2014-11-11T15:16:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kgish](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kgish/32/13333_2.png) [@kgish](https://discuss.emberjs.com/u/kgish)\
**Post date:** [November 11, 2014, 3:54pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664/5 "2014-11-11T15:54:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jdhines](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jdhines/32/12756_2.png) [@jdhines](https://discuss.emberjs.com/u/jdhines)\
**Post date:** [December 2, 2016, 4:00pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664/6 "2016-12-02T16:00:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rtm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/rtm/32/10953_2.png) [@rtm](https://discuss.emberjs.com/u/rtm)\
**Post date:** [December 3, 2016, 1:38pm UTC](https://discuss.emberjs.com/t/how-to-reset-controller-properties-on-exit/6664/7 "2016-12-03T13:38:43Z")

</div>

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.
