📎 Webclip
Ember Gotcha: Controllers Are Singletons
The article describes a bug in an Ember app where starting song creation for one artist and then switching to another leaves the creation UI and partially entered title in place. The reason is that the same controller instance survives route changes, so unrelated controller state does not reset automatically.
Reading notes#
- Controllers in Ember are singletons and are not torn down when leaving a route.
- In the example,
songCreationStartedstays true after moving from one artist to another. - The route changes from one
ArtistSongsRouteto another, but the sameArtistSongsControllerremains in use. - Data bound to the
artistproperty rerenders when the artist changes, but other controller properties stay as they were. canCreateSongdepends onsongCreationStartedor on existing songs.- Because
songCreationStartedstays true, the text input remains visible for the next artist. - One fix is to reset controller properties in
setupController. - The author prefers resetting in the controller with an
artistDidChangeobserver. - The observer clears
newTitleand setssongCreationStartedback to false whenartistchanges.
