↓ Ir para o conteúdo principal

← todas as notas

📎 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, songCreationStarted stays true after moving from one artist to another.
  • The route changes from one ArtistSongsRoute to another, but the same ArtistSongsController remains in use.
  • Data bound to the artist property rerenders when the artist changes, but other controller properties stay as they were.
  • canCreateSong depends on songCreationStarted or on existing songs.
  • Because songCreationStarted stays 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 artistDidChange observer.
  • The observer clears newTitle and sets songCreationStarted back to false when artist changes.