Advertisement
Guest User

Untitled

a guest
Jul 13th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * File:        /lib/***/config.js
  3.  * Application: ***
  4.  * Author:      Albert Scherman
  5.  * Copyright:   (c)2015 ***
  6.  *
  7.  * Router written by John Resig (16-06-2008) (http://ejohn.org/blog/javascript-micro-templating/)
  8.  * Improved by Joakim Carlstein (15-12-2013) (http://joakimbeng.eu01.aws.af.cm/a-javascript-router-in-20-lines/)
  9.  */
  10.  
  11.  
  12. var jsRouter = {
  13.     /**
  14.      * Variables
  15.      */
  16.  
  17.     /**
  18.      * @var el {element} Initializes the element
  19.      */
  20.     el:null,
  21.  
  22.     /**
  23.      * @var routes {Array} Initializes the routes array
  24.      */
  25.     routes:{},
  26.  
  27.     /**
  28.      * @method initialize Initializes the object.
  29.      * Creates event liseteners and sets up routes.
  30.      */
  31.     initialize:function() {
  32.         $(window).on('hashchange',jsRouter.router);
  33.         $(window).on('load',jsRouter.router);
  34.  
  35.         /* ERROR PAGES */
  36.         jsRouter.route('404','errors/404');
  37.         jsRouter.route('500','errors/500');
  38.  
  39.         /* ACTUAL PAGES */
  40.         // Landing Pages
  41.         jsRouter.route('','home');
  42.         jsRouter.route('/','home');
  43.         jsRouter.route('/home','home');
  44.  
  45.         // User Pages
  46.         jsRouter.route('/usr/register','usr/register');
  47.         jsRouter.route('/usr/login','usr/login');
  48.         jsRouter.route('/usr/forgotpassword','usr/forgotpassword');
  49.         jsRouter.route('/usr/activate','usr/activate');
  50.         jsRouter.route('/usr/dashboard','usr/dashboard');
  51.  
  52.         // Address Book Pages
  53.         jsRouter.route('/adr','adr/index',events.adrLoadAddressBooks);
  54.         jsRouter.route('/adr/create','adr/create');
  55.         jsRouter.route('/adr/contact/list','adr/contact/list');
  56.         jsRouter.route('/adr/contact/update','adr/contact/update');
  57.         jsRouter.route('/adr/contact/import','adr/contact/import');
  58.         jsRouter.route('/adr/contact/csv/match','adr/contact/csv/match');
  59.  
  60.         //jsRouter.route('/usr/loggedOut','usr/loggedOut',function() { return auth.getUser(); });
  61.     },
  62.  
  63.     /**
  64.      * @method route Adds a route to the routes array
  65.      * @param path Path in address bar
  66.      * @param template Template file to be loaded (Location: app/templates/)
  67.      * @param controller Data passed to the template for binding
  68.      */
  69.     route:function(path,template,callback) {
  70.         jsRouter.routes[path] = {template: template, callback: callback };
  71.     },
  72.  
  73.     /**
  74.      * @method router Checks the route and creates path to template file
  75.      */
  76.     router:function() {
  77.         app.resetResponse();
  78.         jsRouter.el = jsRouter.el || $('#view');
  79.         var url = $.urlHash() || '/';
  80.         var route = jsRouter.routes[url];
  81.  
  82.         if(typeof route == 'undefined' || typeof route == null) {
  83.             route = jsRouter.routes['404'];
  84.         }
  85.         auth.isLoggedIn();
  86.  
  87.         if(jsRouter.el) {
  88.             if(route.callback) {
  89.                 jsRouter.loadData(config.templates + route.template + '.html',jsRouter.el,route.callback);
  90.             } else {
  91.                 jsRouter.loadPage(config.templates + route.template + '.html',jsRouter.el,"");
  92.             }
  93.         }
  94.     },
  95.  
  96.     /**
  97.      * @method loadPage Loads template, binds data and displays it in the #view element
  98.      * @param page Template file to be loaded
  99.      * @param element Element to display file in
  100.      * @param bindData Data to bind to the template
  101.      */
  102.     loadPage:function(page,element,bindData) {
  103.         $.get(page,function(data) {
  104.             element.html(nano(data, bindData));
  105.             app.setPageListeners();
  106.         });
  107.     },
  108.  
  109.     loadData:function(page,element,callback) {
  110.         if(typeof callback !== 'undefined') {
  111.             if (typeof callback === "function") {
  112.                 callback().done(function(data) {
  113.                     jsRouter.loadPage(page,element,data);
  114.                 });
  115.             } else {
  116.                 alert("Could not call " + endpoint);
  117.             }
  118.         } else {
  119.             jsRouter.loadPage(page,element,this);
  120.         }
  121.     },
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement