Skip to content

Instantly share code, notes, and snippets.

@machty
Last active August 29, 2015 14:02
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 machty/b76b57500b720238b5f3 to your computer and use it in GitHub Desktop.
Save machty/b76b57500b720238b5f3 to your computer and use it in GitHub Desktop.

TL;DR generate changelists of app state mutations that can be used in cases where it's necessary to be able to predict what will happen when some action is invoked, e.g. linking into another route.

I've had this zany idea floating around that I just wanted to share that's probably extremely over-engineered to over-solve problems in the routing domain, but I figured I'd share it and see if any lightbulbs go off.

So in routing land, one of the harder things to calculate is a link's href; we have have to use the provided destination rout and the contexts to provide to basically ask "if this transition were to take place, what URL would you end up with?". This has led to some interesting code with query params, particularly with model dep state, where we have to say "given this destination route, these contexts, this list of explicitly provided query params, please fill sensible default values for unspecified query params and tell me what the final URL is going to be".

Particularly in nailing down these last little API bits of when QPs reset, where default values come from, etc., I keep coming back this potential idea of "changelists", which is a kind of computed property where rather than retrieving dependent values and returning a value, inside your callback, you can set properties, call transitionTo, do all sorts of stuff that impact app state, but rather than immediately causing all of this mutation, the attempted changes are compacted into a changelist, which is an object that describes everything that actually performing that action would do to your app.

This would be very convenient for href generation; rather than exposing a bunch of properties/CPs on the controller for the purpose of assembling into a {{link-to}}'s argument list, in more complex cases you could just pass the linkto a changelist of everything you intend to do when you invoke that link and it'll spit out the href. Heck, maybe we could even use it to sensibly start loading lazily loaded code or something.

Basic gist of the idea:

{{link-to-dynamic crazyAction}}Elsewhere{{/link-to-changelist}}

// on the controller
crazyAction: changelist(function() {
  // `this` is a wrapper around the controller that
  // doesn't actually set properties, but collects them
  // into a changelist that can be used to calculate the
  // href of some complex link-to behavior.

  // page is a QP, demeter forgive me.
  this.set('page', this.get('controllers.session.settings.userLikesResettingPagesUponDoingStuff'));

  // query on destination controller is a query param; this factors
  // into the href that this changelist will be used to generate.
  this.set('controllers.destinationController.query', "wat")

  this.transitionToRoute(someDynamicThing);
})

// based on the changelist generated by all the set actions in the 
// above CP-ish monstrosity, the href for the link is calculated to
// be something like "/elsewhere?query=wat" and if we ever link back 
// to this starting route, its page QP will be remembered to be
// whatever this user's settings tells it to be

This idea is about at a stoner's level of fleshed-out-ness but lemme know if it makes any sense or is obviously bad.

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