Beware of Singletons

You’ll learn about the singleton property of controllers in this lesson.

We'll cover the following

Controllers are singletons

If we play around with the app, we notice something strange. If we start typing a new song title but decide to switch to a new band, the text field stays on the screen with the unfinished song title as its value. This seems weird and is caused by the fact that controllers are singletons in Ember.

What does this mean? It means that each controller class only has one single instance in the application. That instance is created when we enter the corresponding route and is not destroyed when we move away from it.

That explains the above phenomenon: this.showAddSong and this.title are properties on the controller, so they retain their value even when moving to a new page. If that’s not how we want it to be, Ember has us covered by providing a specific hook to update controller properties when moving between routes. It’s called resetController, and it’s called on the route every time a route transition is initiated.

Reset method

We can use this method to reset the properties: