Note: Update to Polymer 2.0
Note: with-auth and with-no-auth attributes is now added on dna-state instead of dna-view.
dna-router is an advance Angular's ui-router style all html router provider for Polymer. With in built authentication management.
Inspired by ui-router: https://github.com/angular-ui/ui-router
Install using bower:
bower install dna-router
Update: Added Vulcanize support.
Use: Add 'manual-load' attribute in 'dna-config' tag and then import the vulcanized templates in your project.
Import all element:
<link rel="import" href="./bower_components/dna-router/dna.html">-
Define states and routes:
<dna-state state='home' route='/home'></dna-state> <dna-state state='user' route='/user/:id/'></dna-state>
If states requires authentication.
<dna-state state='home' with-auth route='/home' else-redirect='login'></dna-view>
If user is not authenticated, it will be redirected to 'login' state.
Show some page only to 'unauthenticated' users
<dna-state state='login' with-no-auth route='/login' else-redirect='home'></dna-state>
State without
authattribute are shown irrespective of auth status -
Defining views. You can have multiple views for a single state.
<dna-view state='home' element='home-template'></dna-view> <dna-view state='home' element='home-another-template'></dna-view>
By default,
dna-viewconvertselementname into camel case and imports file named so in base directory. This file must containhome-templateelement. Example, above imports\homeTemplate.html.All params is available in
home-templatepolymer propertiesparams.To import file from different directory:
<dna-view state='home' template='\templates' element='home-template'></dna-view>
NOTE:Set global template path usingdna-config. -
Configure
dna-router:<dna-config id='conf' home='some state' template='\templates'> </dna-config>
By default
homeis state namedhomeandauthis false.To authosrise on fly using javascript:
DNA.Auth.authorised = true // Then reload var state = DNA.$State() state.reload()
-
dna-aelement: Equivalent toatag in html.- With goto and params property
<dna-a goto="contact">To state X</dna-a>
- You can use a Polymer variable in your params :
<dna-a goto="stateY" params='[[parameterObject]]'>To state Y</dna-a>
- With goto and params property
-
dna-many-viewelement. This element is visible only if anydna-viewinside this element or any of it'sstateis active.<dna-many-view state='abc xyz'> This Example <dna-view state='home' ....></dna-view> </dna-many-view>
In above example, many view is visible for states
abc, xyz and home. For any other state none of its content is visible."This Example"is not visible for some state, i.elogin.
dna-router provides a DNA object.
DNA.run = function(next){
// Do your stuff
// Check for login status in cookies
// Load your assets
next() // Donot forget to call this when using `run` function.
}-
DNA.Auth: Contain authencation information. Can be used to share information accros the app just like a normal javascript variable
-
DNA.$State(): Returns current state and its parameters. Also provide two function
goandreload.var state = DNA.$State() // GO example state.go("next_state_name", parameterObjectOptional) // RELOAD example state.reload()
Enjoy :)