Created
October 31, 2012 13:22
-
-
Save sorbing/3987015 to your computer and use it in GitHub Desktop.
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
/* | |
--- | |
name: Router | |
description: StepCart router manager | |
license: MIT. | |
copyright: Copyright (c) 2011 | |
requires: [] | |
provides: [Router] | |
... | |
*/ | |
var Router = new Class({ | |
// Interfaces | |
Implements: [], | |
options: { | |
routes: { // Тут я хотел хранить все роуты, сейчас они в window.routes | |
'name': 'pattern' | |
} | |
}, | |
// Constructor | |
initialize: function(options) | |
{ | |
console.log('Route initialize...'); | |
/*var myScript = Asset.javascript('/get_routes.js', { | |
id: 'myScript', | |
onLoad: function(){ | |
alert('get_routes.js is loaded!'); | |
} | |
});*/ | |
}, | |
/** | |
* Get route path by name | |
* @param {String} name route name | |
* @param {Object} args route arguments, eq {'id': 123} | |
* @return {String} | |
*/ | |
getRoute: function(name, args) | |
{ | |
if ( ! name) | |
{ | |
console.error('Specify a minimum first artgument for `router.getRoute()` method'); | |
return false; | |
} | |
var pattern = window.routes[name]; | |
//console.log(pattern); | |
if (typeof args == 'object' && pattern !== undefined) | |
{ | |
for (var key in args) | |
{ | |
pattern = pattern.replace(new RegExp('{' + key + '}','g'), args[key]); | |
} | |
} | |
return pattern; | |
} | |
}); | |
document.addEvent("domready", function() { | |
router = new Router(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment