Skip to content

Instantly share code, notes, and snippets.

@erichulburd
Last active September 29, 2016 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichulburd/6c0c766fbec7828fae18a5aaf49958b2 to your computer and use it in GitHub Desktop.
Save erichulburd/6c0c766fbec7828fae18a5aaf49958b2 to your computer and use it in GitHub Desktop.
configuracion de rutas sin clases
import Route from './route';
export function defineRoutes(i18n) {
let routes = [
{
name: 'Home',
path: new RegExp('^\/?((\\w{2})\/?)?$'),
parameters: {2: 'locale'}
}, {
name: 'Dashboard',
path: new RegExp(`^\/?((\\w{2})\/?)?${i18n.t('dashboard')}$`),
parameters: {2: 'locale'}
}, {
name: 'Entries',
path: new RegExp(`^\/?((\\w{2})\/)?${i18n.t('users')}\/(\\d+)/${i18n.t('entries')}`),
parameters: {2: 'locale', 3: 'user_id'},
url: (payload, i18n)=>{
return `/${i18n.language}/${i18n.t('users')}/${payload.user_id}/${i18n.t('entries')}`
}
}, {
name: 'Login',
path: new RegExp(`^\/?((\\w{2})\/)?${i18n.t('login')}$`),
parameters: {2: 'locale'}
}, {
name: 'Register',
path: new RegExp(`^\/?((\\w{2})\/)?${i18n.t('register')}$`),
parameters: {2: 'locale'}
}, {
name: 'Settings',
path: new RegExp(`^\/?((\\w{2})\/)?${i18n.t('settings')}$`),
parameters: {2: 'locale'}
}, {
name: 'User',
path: new RegExp(`^\/?((\\w{2})\/)?${i18n.t('users')}\/(\\d+)$`),
parameters: {2: 'locale', 3: 'user_id'},
url: (payload, i18n)=>{
return `/${i18n.language}/${i18n.t('users')}/${payload.user_id}`
}
}, {
name: 'Users',
path: new RegExp(`^\/?((\\w{2})\/)?${i18n.t('users')}$`),
parameters: {2: 'locale'}
}, {
// Missing route must be last
name: 'Missing',
path: new RegExp(`^\/?((\\w{2})\/?)?.+`),
parameters: {2: 'locale'}
}
].map((definition)=> new Route(definition));
return addHelpers(routes);
}
export function addHelpers(routes){
Object.defineProperty(routes, 'getRoute', {
value: function(route_name){
return this.find( route => route.name === route_name)
},
enumerable: false,
configurable: false
});
return routes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment