Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * File: /lib/***/config.js
- * Application: ***
- * Author: Albert Scherman
- * Copyright: (c)2015 ***
- *
- * Router written by John Resig (16-06-2008) (http://ejohn.org/blog/javascript-micro-templating/)
- * Improved by Joakim Carlstein (15-12-2013) (http://joakimbeng.eu01.aws.af.cm/a-javascript-router-in-20-lines/)
- */
- var jsRouter = {
- /**
- * Variables
- */
- /**
- * @var el {element} Initializes the element
- */
- el:null,
- /**
- * @var routes {Array} Initializes the routes array
- */
- routes:{},
- /**
- * @method initialize Initializes the object.
- * Creates event liseteners and sets up routes.
- */
- initialize:function() {
- $(window).on('hashchange',jsRouter.router);
- $(window).on('load',jsRouter.router);
- /* ERROR PAGES */
- jsRouter.route('404','errors/404');
- jsRouter.route('500','errors/500');
- /* ACTUAL PAGES */
- // Landing Pages
- jsRouter.route('','home');
- jsRouter.route('/','home');
- jsRouter.route('/home','home');
- // User Pages
- jsRouter.route('/usr/register','usr/register');
- jsRouter.route('/usr/login','usr/login');
- jsRouter.route('/usr/forgotpassword','usr/forgotpassword');
- jsRouter.route('/usr/activate','usr/activate');
- jsRouter.route('/usr/dashboard','usr/dashboard');
- // Address Book Pages
- jsRouter.route('/adr','adr/index',events.adrLoadAddressBooks);
- jsRouter.route('/adr/create','adr/create');
- jsRouter.route('/adr/contact/list','adr/contact/list');
- jsRouter.route('/adr/contact/update','adr/contact/update');
- jsRouter.route('/adr/contact/import','adr/contact/import');
- jsRouter.route('/adr/contact/csv/match','adr/contact/csv/match');
- //jsRouter.route('/usr/loggedOut','usr/loggedOut',function() { return auth.getUser(); });
- },
- /**
- * @method route Adds a route to the routes array
- * @param path Path in address bar
- * @param template Template file to be loaded (Location: app/templates/)
- * @param controller Data passed to the template for binding
- */
- route:function(path,template,callback) {
- jsRouter.routes[path] = {template: template, callback: callback };
- },
- /**
- * @method router Checks the route and creates path to template file
- */
- router:function() {
- app.resetResponse();
- jsRouter.el = jsRouter.el || $('#view');
- var url = $.urlHash() || '/';
- var route = jsRouter.routes[url];
- if(typeof route == 'undefined' || typeof route == null) {
- route = jsRouter.routes['404'];
- }
- auth.isLoggedIn();
- if(jsRouter.el) {
- if(route.callback) {
- jsRouter.loadData(config.templates + route.template + '.html',jsRouter.el,route.callback);
- } else {
- jsRouter.loadPage(config.templates + route.template + '.html',jsRouter.el,"");
- }
- }
- },
- /**
- * @method loadPage Loads template, binds data and displays it in the #view element
- * @param page Template file to be loaded
- * @param element Element to display file in
- * @param bindData Data to bind to the template
- */
- loadPage:function(page,element,bindData) {
- $.get(page,function(data) {
- element.html(nano(data, bindData));
- app.setPageListeners();
- });
- },
- loadData:function(page,element,callback) {
- if(typeof callback !== 'undefined') {
- if (typeof callback === "function") {
- callback().done(function(data) {
- jsRouter.loadPage(page,element,data);
- });
- } else {
- alert("Could not call " + endpoint);
- }
- } else {
- jsRouter.loadPage(page,element,this);
- }
- },
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement