Advertisement
Guest User

Other option

a guest
Aug 2nd, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.KDJ || (window.KDJ = {});    //namespace
  2. KDJ.controller = (function () {
  3.     $(document).off('PAGE_DISPLAYED');
  4.     $(document).on('PAGE_DISPLAYED', function (e, data) {
  5.         if(data.customFunction !== undefined) {
  6.             //do things if you want
  7.  
  8.             data.customFunction();
  9.  
  10.             //do things if you want
  11.  
  12.             // here I use the return to pass by the switch
  13.             //you could use another property like if(data.useSwitch) => switch() or simply use an else clause
  14.             return;
  15.         }
  16.         switch(data.pageId) {
  17.             case "page1":
  18.                 // do something..
  19.                 break;
  20.             default:
  21.            
  22.                 break;
  23.         }
  24.     });
  25.  
  26.     return {
  27.         // exported methods and props..
  28.     };
  29. })();
  30.  
  31.  
  32. KDJ.view = (function () {  
  33.     // ...
  34.     $(document).off('pagechange');
  35.  
  36.     //This object its optional, you could pass it explicitly everywhere.
  37.     var pageDisplayedObj = {
  38.         'pageId': loadedPageId,
  39.         'key': key,
  40.         customFunction: function() {}
  41.     }
  42.     $(document).on('pagechange', function (e, ui) {
  43.        
  44.         //I redefine customFunction to do what I need, again, you could pass pageDisplayedObj explicitly
  45.         pageDisplayedObj.customFunction = function(ui) {
  46.                 console.log("I'm doing something awesome with the ui!");
  47.         };
  48.         $(document).trigger(PAGE_DISPLAYED, pageDisplayedObj);
  49.  
  50.     });
  51.  
  52.     $(document).on('pagechange', function (e, ui) {
  53.        
  54.         //here I'll use the switch
  55.         pageDisplayedObj.customFunction = undefined;
  56.         $(document).trigger(PAGE_DISPLAYED, pageDisplayedObj);
  57.  
  58.     });
  59. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement