Created
July 17, 2012 21:15
-
-
Save typeofweb/3132162 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var routeModule = function (module) { | |
var routes = {}; | |
routes[module + '/*url'] = 'handleUrl'; | |
routes[module] = 'handleUrl'; | |
var ModuleRouter = Backbone.Router.extend({ | |
routes: routes, | |
handleUrl: function (url) { | |
require(['modules/' + module + '/' + module], function (myModule) { | |
url = url || ''; | |
if (url[url.length - 1] === '/') { | |
url = url.substr(0, url.length - 1); | |
} | |
var sUrl = url.split('/'), | |
ctrl = sUrl.splice(0, 1)[0]; | |
if (myModule) { | |
if (myModule.hasOwnProperty(ctrl) && typeof myModule[ctrl] === 'function' && ctrl.indexOf('_') !== 0) { | |
// controller inside the module exists | |
console.log(module + '.' + ctrl + '(' + sUrl.toString() + ')'); | |
myModule[ctrl].apply(null, sUrl); | |
} else if (myModule.defaultAction) { | |
// module exists, defaul action is called | |
console.log(module + '.defaultAction(' + sUrl.toString() + ')'); | |
myModule.defaultAction.apply(null, ((sUrl && ctrl) ? sUrl.concat(ctrl) : [])); | |
} else { | |
console.log('404 in ' + module); | |
} | |
} else { | |
console.log('404 no ' + module); | |
} | |
}); | |
} | |
}); | |
return new ModuleRouter(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment