Automatically render to the body or html element
Switch branches/tags
0.8-legacy 138-import-can-stache-bindings debug debug2 default-vm err greenkeeper/can-component-4.1.0 greenkeeper/can-component-4.1.3 greenkeeper/can-component-4.2.0 greenkeeper/can-component-4.2.1 greenkeeper/can-component-4.2.2 greenkeeper/can-component-4.2.3 greenkeeper/can-component-4.2.4 greenkeeper/can-component-4.3.0 greenkeeper/can-component-4.4.0 greenkeeper/can-component-4.4.2 greenkeeper/can-component-4.4.4 greenkeeper/can-component-4.4.5 greenkeeper/can-component-4.4.6 greenkeeper/can-component-4.4.7 greenkeeper/can-component-4.4.8 greenkeeper/can-component-4.4.9 greenkeeper/can-component-4.4.10 greenkeeper/can-component-pin-4.0.8 greenkeeper/can-component-pin-4.1.2 greenkeeper/can-component-pin-4.4.4 greenkeeper/can-define-2.2.2 greenkeeper/can-define-2.2.4 greenkeeper/can-define-2.2.5 greenkeeper/can-define-2.3.0 greenkeeper/can-define-2.3.1 greenkeeper/can-define-2.3.6 greenkeeper/can-define-2.3.7 greenkeeper/can-define-2.4.0 greenkeeper/can-define-2.5.0 greenkeeper/can-define-2.5.1 greenkeeper/can-define-2.5.2 greenkeeper/can-define-2.5.3 greenkeeper/can-define-2.5.4 greenkeeper/can-define-2.5.5 greenkeeper/can-define-2.5.6 greenkeeper/can-define-2.5.8 greenkeeper/can-define-2.5.9 greenkeeper/can-define-2.5.10 greenkeeper/can-define-2.6.0 greenkeeper/can-define-2.6.1 greenkeeper/can-define-2.6.2 greenkeeper/can-define-pin-2.2.1 greenkeeper/can-define-pin-2.2.4 greenkeeper/can-define-pin-2.3.5 greenkeeper/can-define-pin-2.5.12 greenkeeper/can-dom-mutate-1.0.4 greenkeeper/can-dom-mutate-1.1.0 greenkeeper/can-dom-mutate-1.1.1 greenkeeper/can-dom-mutate-1.2.0 greenkeeper/can-dom-mutate-1.2.1 greenkeeper/can-dom-mutate-1.3.0 greenkeeper/can-dom-mutate-1.3.1 greenkeeper/can-dom-mutate-1.3.2 greenkeeper/can-dom-mutate-1.3.3 greenkeeper/can-dom-mutate-1.3.4 greenkeeper/can-dom-mutate-1.3.5 greenkeeper/can-dom-mutate-1.3.6 greenkeeper/can-dom-mutate-pin-1.0.3 greenkeeper/can-dom-mutate-pin-1.0.4 greenkeeper/can-dom-mutate-pin-1.2.2 greenkeeper/can-reflect-1.16.3 greenkeeper/can-reflect-1.17.0 greenkeeper/can-reflect-1.17.1 greenkeeper/can-reflect-1.17.2 greenkeeper/can-reflect-1.17.3 greenkeeper/can-reflect-1.17.4 greenkeeper/can-reflect-1.17.5 greenkeeper/can-reflect-pin-1.16.2 greenkeeper/can-reflect-pin-1.16.7 greenkeeper/can-route-4.2.0 greenkeeper/can-route-4.3.0 greenkeeper/can-route-4.3.1 greenkeeper/can-route-4.3.2 greenkeeper/can-route-4.3.3 greenkeeper/can-route-4.3.4 greenkeeper/can-route-4.3.5 greenkeeper/can-route-4.3.6 greenkeeper/can-route-4.3.7 greenkeeper/can-route-4.3.8 greenkeeper/can-route-4.3.9 greenkeeper/can-route-4.3.10 greenkeeper/can-route-4.3.11 greenkeeper/can-route-4.3.12 greenkeeper/can-route-4.4.6 greenkeeper/can-route-pin-4.1.2 greenkeeper/can-route-pin-4.4.5 greenkeeper/can-stache-4.2.2 greenkeeper/can-stache-4.2.3 greenkeeper/can-stache-4.2.4 greenkeeper/can-stache-4.2.5 greenkeeper/can-stache-4.3.1 greenkeeper/can-stache-4.3.4 greenkeeper/can-stache-4.6.0 greenkeeper/can-stache-4.7.0 greenkeeper/can-stache-4.7.1 greenkeeper/can-stache-4.8.0 greenkeeper/can-stache-4.9.0 greenkeeper/can-stache-4.9.1 greenkeeper/can-stache-4.10.0 greenkeeper/can-stache-4.10.3 greenkeeper/can-stache-4.10.4 greenkeeper/can-stache-4.10.5 greenkeeper/can-stache-4.15.1 greenkeeper/can-stache-4.15.2 greenkeeper/can-stache-4.15.3 greenkeeper/can-stache-4.15.4 greenkeeper/can-stache-4.15.5 greenkeeper/can-stache-4.15.6 greenkeeper/can-stache-4.15.7 greenkeeper/can-stache-4.15.8 greenkeeper/can-stache-4.15.9 greenkeeper/can-stache-4.15.11 greenkeeper/can-stache-4.15.12 greenkeeper/can-stache-bindings-4.5.1 greenkeeper/can-stache-bindings-4.6.0 greenkeeper/can-stache-bindings-4.6.1 greenkeeper/can-stache-bindings-4.6.2 greenkeeper/can-stache-bindings-4.6.3 greenkeeper/can-stache-bindings-4.6.4 greenkeeper/can-stache-bindings-4.6.5 greenkeeper/can-stache-bindings-4.7.0 greenkeeper/can-stache-bindings-4.7.1 greenkeeper/can-stache-bindings-4.7.2 greenkeeper/can-stache-bindings-4.7.3 greenkeeper/can-stache-bindings-pin-4.5.0 greenkeeper/can-stache-pin-4.2.1 greenkeeper/can-stache-pin-4.3.0 greenkeeper/can-stache-pin-4.5.10 greenkeeper/can-stache-pin-4.15.0 greenkeeper/can-util-3.11.7 greenkeeper/can-util-3.12.0 greenkeeper/can-util-pin-3.11.6 greenkeeper/can-view-import-4.1.0 greenkeeper/can-view-import-4.2.0 greenkeeper/can-view-import-pin-4.0.2 greenkeeper/can-view-scope-4.5.0 greenkeeper/can-view-scope-4.7.0 greenkeeper/can-view-scope-4.7.1 greenkeeper/can-view-scope-4.11.0 greenkeeper/can-view-scope-4.11.1 greenkeeper/can-view-scope-4.12.0 greenkeeper/can-view-scope-4.12.1 greenkeeper/can-view-scope-4.13.0 greenkeeper/can-view-scope-pin-4.4.3 greenkeeper/can-view-scope-pin-4.10.1 greenkeeper/funcunit-3.6.0 greenkeeper/funcunit-3.6.1 greenkeeper/funcunit-pin-3.5.0 greenkeeper/steal-2.1.6 greenkeeper/steal-2.1.7 greenkeeper/steal-2.1.8 greenkeeper/steal-2.1.9 greenkeeper/steal-2.1.10 greenkeeper/steal-pin-2.1.5 greenkeeper/steal-tools-2.0.8 greenkeeper/steal-tools-2.0.9 greenkeeper/steal-tools-2.0.10 greenkeeper/steal-tools-2.0.11 greenkeeper/steal-tools-pin-2.0.7 greenkeeper/update-to-node-10 incheaders keep-data major master rev slim-loader
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.editorconfig
.gitignore
.npmrc
.travis.yml
LICENSE.md
README.md
build.js
no-zone.js
package.json

README.md

Build Status npm version Greenkeeper badge

done-autorender

Automatically renders a template, either to the <html> or <body> elements.

Install

Install with NPM and use with StealJS:

npm install done-autorender --save

Use

done-autorender enables you to use a Stache template as your application entry-point (the main). done-autorender will wait for your page to be fully loaded (including all dependencies) and then will insert the template into the <head> and <body>. For example:

index.stache

<html>
<head>
  <title>My Site</title>
</head>
<body>
  <can-import from="~/main.css" />
  <can-import from="~/routes" />
  <can-import from="~/state" export-as="viewModel" />

  {{#eq(page, "home")}}
    <can-dynamic-import from="~/home/">
      {{#if(./isResolved)}}
        <home-page></home-page>
      {{/if}}
    </can-dynamic-import>
  {{/eq}}
</body>
</html>

index.html

<script src="node_modules/steal/steal.js"
	main="app/index.stache!done-autorender" main></script>

Then load index.html in a browser. After all dependencies are loaded your index.stache will be rendered and inserted into the page.

No Zone option

If you do not use SSR but still want to use done-autorender to bootstrap your application you can use the no-zone module:

<script src="node_modules/steal/steal.js"
	main="app/index.stache!done-autorender/no-zone" main></script>

API

export-as

Each done-autorender application is backed by a ViewModel (such as a DefineMap) that represents the state of the entire application.

To import this View Model into your application use a can-import tag like so:

<can-import from="todo-app/app" export-as="viewModel" />

The export-as attribute specifies that this module represents the viewModel. The todo-app module might look like:

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

route.urlData = new RoutePushstate();
route.register("{page}", { page: "home" });

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

export default AppViewModel;

Internally done-autorender will create a new instance of this DefineMap and set it as the route.data.

route-data

Specifies an alternative property on the ViewModel to use as the route data.

Using the default route.data

Starting in can-route 4.4.0 you can more easily use the default route.data, as it is a DefineMap.

To use this pattern, first add a property on your ApplicationViewModel that is the route.data property:

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

route.urlData = new RoutePushstate();
route.register("{page}", { page: "home" });

const AppViewModel = DefineMap.extend("AppViewModel", {
	routeData: {
		default: () => route.data
	}
});

export default AppViewModel;

Now, in your index.stache, set the route-data attribute to this property name:

<can-import from="todo-app/app" export-as="viewModel" route-data="routeData" />

From here you can you use the properties on route.data the same way you can any other ViewModel. Here's a fuller example template:

<html>
<head>
  <title>{{routeData.page}} | My App</title>
</head>
<body>
  <can-import from="todo-app/app" export-as="viewModel" route-data="routeData" />

  <h1>My App!</h1>
  <h2>{{routeData.page}}</h2>
</body>
</html>

Using a custom Type

Since route-data allows you to specify any property on your ViewModel, one option is to use a custom type, such as a DefineMap. This allows you to separate properties that you want attached to the route from other properties on the ViewModel that you do not.

Below is an example AppViewModel module:

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

route.urlData = new RoutePushstate();
route.register("{page}", { page: "home" });

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

const AppViewModel = DefineMap.extend("AppViewModel", {
	routeData: {
		Default: MyRouteData
	}
});

export default AppViewModel;

And then to use it, set it as the route-data attribute:

<html>
<head>
  <title>{{routeData.page}} | My App</title>
</head>
<body>
  <can-import from="todo-app/app" export-as="viewModel" route-data="routeData" />

  <h1>My App!</h1>
  <h2>{{routeData.page}}</h2>
  <p>This is coming from the <strong>MyRouteData</strong> observable!</p>
</body>
</html>

Debugging

In development mode, can-zone/debug is enabled to provide debugging information in the case where the Zone times out and the app is never attached to the page.

You can control the timeout by setting the data-autorender-timeout attribute on the steal script tag (note that data- is optional here) like so:

<script src="./node_modules/steal/steal.js"
	data-autorender-timeout="1000"></script>

The timeout is specified in milliseconds; in the above example it is 1 second. By default the zone will timeout after 5000 milliseconds (5 seconds).

After the Zone has timed out the console will print out stack traces of code that failed to complete. Use these stack traces to figure out what caused reattachment to fail and fix anything that can be fixed (by, for example, using Zone.ignore). These information looks like:

debug stack traces

Break on timeout

Additionally you can choose to have a debugger; breakpoint that is hit after the Zone times out. This allows you to more easily figure out which code is still running and see if something can be done about it.

Enable this by setting the data-autorender-debug-break option on the steal script tag like so:

<script src="./node_modules/steal/steal.js"
	data-autorender-debug-break></script>

This is a boolean attribute and doesn't need a value. Instead of logging stack traces you will get a breakpoint from which you can look back in the call stack and inspect the reason why the code continued to run past the timeout.

break on timeout

can-view-model

If you install can-view-model you can use it to access the Application ViewModel like so:

var canViewModel = require("can-view-model");

var appVM = canViewModel(document.documentElement);

Keeping elements in the DOM

By default done-autorender removes all elements from the <head> and <body> and replaces them with the elements from the template. This is to facilitate use with done-ssr which will have duplicated content.

Some times, especially if not using done-ssr, you might want to keep some elements that are in your HTML but not your stache template. Use the data-keep property and done-autorender will leave them alone:

<html>
<head>
  <meta name="some-prop" content="some-value" data-keep>
</head>
</html>

connectedCallback

done-autorender supports the connectedCallback lifecycle hook, and it works the same way as in can-component.

The callback function receives the document.documentElement (aka the <html> element) as its only argument. You can use this.listenTo to listen to changes in the DefineMap's properties, or to listen to events in the DOM.

The callback should return a function that will be called when the document is torn down, where cleanup should be done.

The following is an example counter that is implemented using connectedCallback:

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

export default DefineMap.extend({
	count: {
		type: 'number',
		default: 0,
		serialize: false
	},

	connectedCallback(el) {
		const button = el.querySelector("#increment");

		this.listenTo(button, "click", () => {
			this.count++;
		});

		return () => this.stopListening();
	}
});