Skip to content

Instantly share code, notes, and snippets.

@martypdx
Last active August 29, 2015 14:18
Show Gist options
  • Save martypdx/fff5530c1fa783f933cd to your computer and use it in GitHub Desktop.
Save martypdx/fff5530c1fa783f933cd to your computer and use it in GitHub Desktop.
super simple router
<header> some page header </header>
{{#if page}}
<page/>
{{/if}}
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);
}
});
}
});
<header> some page header </header>
{{>getComponent(name)}}
// 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;
}
},
<!-- 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}}
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