Advertisement
Guest User

single

a guest
Feb 13th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jslint browser: true*/
  2. /*global $, jQuery, alert, form, table*/
  3. (function iife() {
  4.     'use strict';
  5.     /**
  6.      * Imports html content in div with id view
  7.      * @param data
  8.      */
  9.     function importInView(data) {
  10.         $('#view').html(data);
  11.     }
  12.  
  13.     /**
  14.      * Extends import in view by adding some events
  15.      * @param data
  16.      */
  17.     function importInViewFormDataAndEvents(data){
  18.         importInView.call(this, data);
  19.  
  20.         if(table.editMode() === true){
  21.             form.fillData(table.getCars(), table.getCarToEdit());
  22.         }
  23.  
  24.         $('#submitButton').on('click', form.validate).on('click', function() {
  25.             table.addCar(form.getData());
  26.         });
  27.     }
  28.  
  29.     /**
  30.      * Extends import in view by redrawing table content
  31.      * then adds some events to table
  32.      * @param data
  33.      */
  34.     function importInViewTableDataAndEvents(data){
  35.         importInView.call(this, data);
  36.  
  37.         table.drawTable();
  38.  
  39.         $('table').on('click', table.editData);
  40.     }
  41.  
  42.     /**
  43.      * Makes ajax request for some url and on success imports in in view
  44.      */
  45.     function ajaxRequestWithCallback(url, callback) {
  46.         $.ajax(url).done(callback);
  47.     }
  48.  
  49.     /**
  50.      * Makes routing according to hash change
  51.      */
  52.     function routing() {
  53.         switch (document.location.hash) {
  54.             case '':
  55.                 ajaxRequestWithCallback('views/home.html', importInView);
  56.                 break;
  57.             case "#addCar":
  58.                 ajaxRequestWithCallback('views/form.html', importInViewFormDataAndEvents);
  59.                 break;
  60.             case "#allCars":
  61.                 ajaxRequestWithCallback('views/table.html', importInViewTableDataAndEvents);
  62.                 break;
  63.             case "#editCar":
  64.                 ajaxRequestWithCallback('views/form.html', importInViewFormDataAndEvents);
  65.                 break;
  66.             default:
  67.                 ajaxRequestWithCallback('views/pageNotFound.html', importInView);
  68.                 break;
  69.         }
  70.     }
  71.  
  72.     /**
  73.      * Adds event listener to window on load event and on window hash change event.
  74.      */
  75.     function addEventListeners() {
  76.         $(window).load(routing);
  77.         $(window).bind('hashchange', routing);
  78.     }
  79.  
  80.     addEventListeners();
  81. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement