map
can.route.map
Assign a can.Map instance that acts as can.route's internal can.Map. The purpose for this is to cross-bind a top level state object (Application State) to the can.route.
can.route.map(MapConstructor)
Parameters
-
MapConstructor
{can.Map}
A can.Map constructor function. A new can.Map instance will be created and used as the can.Map internal to can.route.
can.route.map(mapInstance)
Parameters
-
mapInstance
{can.Map}
A can.Map instance, used as the can.Map internal to can.route.
Background
One of the biggest challenges in a complex application is getting all the different parts of the app to talk to each other simply, cleanly, and reliably.
An elegant way to solve this problem is using the Observer Pattern. A single object, which can be called Application State, holds the high level state of the application.
Use
can.route.map
provides an easy to way make your Application State object cross-bound tocan.route
, using an internal can.Map instance, which is serialized into the hash (or pushstate URLs).When to call it
Call
can.route.map
at the start of the application lifecycle, before any calls tocan.route.bind
. This is becausecan.route.map
creates a new internalcan.Map
, replacing the defaultcan.Map
instance, so binding has to occur on this new object.Demo
The following shows creating an appState that loads data at page load, has a virtual property 'locationIds' which serializes an array, and synchronizes the appState to can.route:
Using arrays and can.Lists
If the Application State contains a property which is any non-primitive type, its useful to use the define plugin to define how that property will serialize.
can.route
calls serialize internally to turn the Application State object into URL params.The following example shows a flags property, which is an array of string-based flags:
Complete example
The following example shows loading some metadata on page load, which must be loaded as part of the Application State before the components can be initialized
It also shows an example of a "virtual" property on the AppState, locationIds, which is the serialized version of a non-serializeable can.List, locations. A setter is defined on locationIds, which will translate changes in locationIds back to the locations can.List.
Why
The Application State object, which is cross-bound to the can.route via
can.route.map
and represents the overall state of the application, has several obvious uses: