@matthewp matthewp released this Sep 12, 2018 · 23 commits to master since this release

Assets 2

This is a minor release of can-route that adds DefineMap, from can-define, as the new default for route.data. This is backwards compatible with old uses of the default route.data.

To see how this is useful, consider this example:

import route from "can-route";

route.register("{page}", { page: "home" });
route.start();

console.log(route.data.page); // -> "home"

As you can see, since route.data is a DefineMap we can use the properties on it directly using JavaScript dot notation.

The purpose of this change is to reduce the amount of boilerplate to get started with canjs routing. Usually you create an application ViewModel and then set it as the route.data like below.

import route from "can-route";
import DefineMap from "can-define/map/map";

const AppViewModel = DefineMap.extend({
  page: "string"
});

route.register("{page}", { page: "home" });
route.data = new AppViewModel();
route.start();

console.log(route.data.page); // -> "home"

The above still works, of course, but with can-route 4.2.0 this boilerplate becomes unnecessary.

This also allows you to have better separation between your application state and your route data. No more serialize: false!! Consider the following:

import route from "can-route";
import DefineMap from "can-define/map/map";

const AppViewModel = DefineMap.extend({
  someNonSerializedState: "string",

  routeData: {
    default: () => route.data
  }
});

route.register("{page}", { page: "home" });

Now you have clean separate between your route state and the rest of your application's state. You can use this in your stache like so:

<some-component page:from="routeData.page" />

{{someNonSerializedState}}