Skip to content

Instantly share code, notes, and snippets.

@machty
Last active October 9, 2016 15:32
Show Gist options
  • Save machty/9531165 to your computer and use it in GitHub Desktop.
Save machty/9531165 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/machty and /u/machty/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