Maintaining component state through route transitions - EmberJS


#1

I am building a single page music web app using Ember. Each track is represented on the page as a component. There are many tracks on a given page. When a user clicks play, the component updates its UI to reflect that and the master route keeps track of which track is currently playing.

But when I switch routes to explore other parts of the app, and then return to the one where the track is playing, Ember has destroyed and rebuilt each component, setting everything back to its initial state and making the route’s current_track object obsolete.

How would I best maintain state like this across route transitions?

My route hierarchy:

master-route 
    liked-tracks
    favorite-tracks
    posted-tracks

Another way to think of this is: on soundcloud, you play a track, then navigate to a different route. When you navigate back, the playing element hasn’t lost its state or been rebuilt. I.e. the progress bar for the song isn’t reset. It’s still where it should be in relation to how far through the song you are.


#2

This is the perfect place to use services. Instead of the master route keeping track of the playing track, keep track of that state in a service. You can inject that service into your track component to identify whether that track is playing or not.


#3

Sounds like what ember-state-services solves, https://github.com/stefanpenner/ember-state-services