Skip to content

Instantly share code, notes, and snippets.

@iStefo
Forked from machty/model-dep-qp.md
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iStefo/11187893 to your computer and use it in GitHub Desktop.
Save iStefo/11187893 to your computer and use it in GitHub Desktop.

"Model-dependent query param stickiness"

The question of whether query param controller properties are preserved/restored/reset when navigating away/back/between routes is a tricky one, but the sensible default seems to be "model-dependent stickiness", whereby query params are stick unless you're switching the model of the current or parent routes. There's more that could be said about this but here's a quick summary of my plan:

  • We need a dictionary to store QP state scoped to a model
  • The keys to this dictionary should be URL path fragments (substrings)

So, given:

Router.map(function() {
  this.resource('user', { path: '/u/:username' }, function() {
    this.route('comments', { path: '/c/:comment_id' });
    this.route('friend',   { path: '/f/:friend_id' });
  });
  this.resource('article', { path: '/a/:article_id' }, function() {
    this.route('comments', { path: '/c/:comment_id' });
  });
});

At url /u/machty/c/123, the current QP value for any QP props in UserController will be stored in a dictionary stored within UserRoute with a key of /u/machty, and any QPs on UserCommentsController will be stored on UserCommentsRoute as /u/machty/c/123.

The result of this is that once the user changes to ebryn, the dictionary lookup will fail (because the keys are now /u/ebryn and /u/ebryn/c/whatever, respectively), so the default QP values will be used for any link-to / transitionTos into that user or any of its children routes. As a corollary, routes with no dynamic segments don't have to reset their controller props to default values because its considered to have the same model; Route#serialize is essentially used as a hash key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment