Last active
August 29, 2015 14:18
-
-
Save martypdx/fff5530c1fa783f933cd to your computer and use it in GitHub Desktop.
super simple router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<header> some page header </header> | |
{{#if page}} | |
<page/> | |
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new Ractive({ | |
el: '#app', | |
template: '#template', | |
data: { | |
page: false | |
}, | |
components: { | |
page: function(){ | |
var route = this.get('route'); | |
return this.components[route || 'home']; | |
} | |
}, | |
oninit: function(){ | |
this.observe('route', function(route){ | |
if(route){ | |
this.reset({ | |
page: true, | |
route: route | |
}); | |
} else { | |
this.set('page', false); | |
} | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<header> some page header </header> | |
{{>getComponent(name)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requires including parsing at runtime | |
data: { | |
getComponent: function(view){ | |
if(!view) { return 'loading'; } | |
if(!!this.partials[view]) return view; | |
this.partials[view] = '<' + view + ' board="{{board}}" user="{{user}}" isRoot="true"/>'; | |
return view; | |
} | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- routes could be managed explicitly in the template --> | |
<header> some page header </header> | |
{{#if page === 'contact'}} | |
<contact/> | |
{{else if page === 'settings'}} | |
<settings/> | |
{{else if page === 'whatever'}} | |
<dance-break/> | |
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onhashchange = function load(){ | |
var hash = location.hash.replace('#','').split('/'), | |
View = Ractive.components[hash[0]] || Ractive.components.home; | |
new View({ | |
el: '#app', | |
data: { id: hash[1] } | |
}); | |
} | |
load(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment