Global Query Params for Deeply Stateful Ember Apps

Matt Gardner
Nov 27, 2017 · 2 min read

One perennial issue we run into developing applications at NYC City Planning is the need for persisting deep application state within a URL. It is sometimes an overlooked but powerful feature to share a stateful hyperlink with other colleagues. This is especially true for interactive mapping applications where dozens of map layers and layer-specific filtering logic requires complex and carefully-named query parameters.

For our upcoming rework of ZoLa, the city’s zoning and land use mapping application, we needed a way to easily pass around a large mapping state object with numerous properties. As our JavaScript framework of choice, EmberJS ships with query params for free, but our need for deep and complex statefulness would’ve lent itself to very long and un-maintainable controllers, an issue others building similar apps have raised.

Joshua Hornby proposes using Ember Services (global singletons):

Here, a “date” service contains all query parameter state. The developer uses computed aliases to make them more accessible in whichever classes they need.

I’d propose something similar, but much simpler:

Here, we avoid the added cruft of aliasing properties from the service (and therefore re-creating the original problem) and name the controller’s query parameters directly. This approach, combined with the excellent Ember Parachute, will make things much more manageable in situations where deep and complex state must be preserved in the URL.

On top of that, you can even organize query parameters into a nested object. This can help with name-spacing the parameters. Here’s an example of a service:

This way, we are able to reference nested properties. For a working example, see this Ember Twiddle.

NYC Planning Digital

Notes on data, engineering, and design @NYCPlanning

Matt Gardner

Written by

Urbanism, web & GIS.

NYC Planning Digital

Notes on data, engineering, and design @NYCPlanning

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade