This repository has been archived by the owner on Sep 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathiron-lazy-pages.js
89 lines (73 loc) · 2.43 KB
/
iron-lazy-pages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
@license
Copyright (C) 2016, Tim van der Lippe
All rights reserved.
This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
*/
import '@polymer/polymer/polymer-legacy.js';
import { IronLazyPagesBehavior } from './iron-lazy-pages-behavior.js';
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
/**
Big applications have a lot of pages. On first load, loading all page elements
is undesirable. Most of the pages are unused for the current user. To solve
these performance issues, lazy-loading provides an easy-to-use solution.
Lazy-loading means that all elements of your page are loaded when the user
opens the respective page. E.g. when your user visits `domain.com/about`, all
elements on the about page are fetched and loaded.
Example:
```html
<iron-lazy-pages attr-for-selected="data-route" selected="{{route}}">
<x-foo data-route="foo" data-path="demo/x-foo.html"></x-foo>
<x-bar data-route="bar" data-path="demo/x-bar.html"></x-bar>
<section data-route="baz">
Inline element baz.
</section>
</iron-lazy-pages>
```
In the above example, whenever the user routes to `domain.com/foo`, the elements defined
in `foo/foo.html` are fetched from the server and loaded by Polymer.
Consequently whenever the selected value changes from `foo` to `bar`, the page `foo`
will be hidden.
Fetching is only performed once, e.g. switching from `foo` to `bar` to `foo` will fetch
`foo` once and show `foo` twice.
## `<dom-if>` support
You can also add `<dom-if>` as a route to enable restamping:
```html
<iron-lazy-pages
attr-for-selected="data-route"
selected="{{route}}"
loading="{{loading}}"
hide-immediately>
<template is="dom-if" data-route="foo" restamp>
Leaving this tab and coming back will loose input value due to restamp<br/>
<input type="text"/>
</template>
<template is="dom-if" data-route="bar">
Leaving this tab and coming back will keep input value<br/>
<input type="text"/>
</template>
</iron-lazy-pages>
```
@group Iron Elements
@element iron-lazy-pages
@demo demo/index.html
*/
Polymer({
_template: html`
<style>
:host {
display: block;
}
:host > ::slotted(:not(.iron-lazy-selected)) {
display: none !important;
}
</style>
<slot></slot>
`,
is: 'iron-lazy-pages',
behaviors: [
IronLazyPagesBehavior
]
});