Advertisement
Palid

JS HSIndicator review

Sep 7th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Should be a module, not a var. Example:
  2. //;(function($){})(jQuery)
  3. var HSIndicator = (function(url){
  4.  
  5.     var callbacks = {
  6.         onOpen : [],
  7.         onClosed : [],
  8.         error : [],
  9.         retry : []
  10.         },
  11.  
  12.         versionConverters = {
  13.             '0.8' : function(data) {
  14.                 return { state : { open : data.open,
  15.                                    lastchange : data.lastchange,
  16.                                    icon : { open : undefined, closed : undefined },
  17.                                    message : data.status,
  18.                                    trigger_person : undefined}};
  19.             },
  20.             '0.9' : function(data) {
  21.                 return { state : { open : data.open,
  22.                                    lastchange : data.lastchange,
  23.                                    icon : { open : undefined, closed : undefined },
  24.                                    message : data.status,
  25.                                    trigger_person : undefined}};
  26.             },
  27.             '0.11' : function(data) {
  28.                 return { state : { open : data.open,
  29.                                    lastchange : data.lastchange,
  30.                                    icon : data.icon,
  31.                                    message : data.status,
  32.                                    trigger_person : undefined}};
  33.             },
  34.             '0.12' : function(data) {
  35.                 return { state : { open : data.open,
  36.                                    lastchange : data.lastchange,
  37.                                    icon : data.icon,
  38.                                    message : data.status,
  39.                                    trigger_person : undefined}};
  40.             },
  41.             '0.13' : function(data) {
  42.                 return data;
  43.                 //missing comma
  44.             }}/* HERE */
  45.         //it's enough to just declare variable
  46.         //declared variable but not set as anything will STILL be
  47.         //an undefined one. Don't ask me how, it just works like that.
  48.         timer = undefined;
  49.  
  50.     function resolve() {
  51.         //IMHO jQuery is an overkill when using JUST XMLHttpRequest :/
  52.         jQuery.ajax({
  53.             type : 'GET',
  54.             url : url,
  55.             beforeSend : function() {
  56.                 callbacks.retry.forEach(function(what) { what(); });
  57.             }
  58.         }).done(function(result) {
  59.             data = versionConverters[result.api](result);
  60.             if(data.state.open) {
  61.                 callbacks.onOpen.forEach(function(what) { what(data); });
  62.             }
  63.             else {
  64.                 callbacks.onClosed.forEach(function(what) { what(data); });
  65.             }
  66.         }).fail(function(jqXHR, errText, err) {
  67.             callbacks.error.forEach(function(what) { what(errText, err); });
  68.         });
  69.         return this; //After first setInterval this == window.
  70.         //Also, it's not used anywhere so returning it is pretty useless.
  71.         //I know that it should work during init only, but prototypes does it
  72.         //better in this cause you can be sure that your context will stay the
  73.         //same.
  74.     };/* Unnecessary semicolon (functions doesn't need them).
  75.          Use JSHint. */
  76.  
  77.     function onOpen(callback) {
  78.         callbacks.onOpen.push(callback);
  79.         return this; //After first setInterval this == window.
  80.         //Also, it's not used anywhere so returning it is pretty useless.
  81.         //I know that it should work during init only, but prototypes does it
  82.         //better in this cause you can be sure that your context will stay the
  83.         //same.
  84.     };/* Unnecessary semicolon (functions doesn't need them).
  85.          Use JSHint. */
  86.  
  87.     function onClosed(callback) {
  88.         callbacks.onClosed.push(callback);
  89.         return this; //After first setInterval this == window.
  90.         //Also, it's not used anywhere so returning it is pretty useless.
  91.         //I know that it should work during init only, but prototypes does it
  92.         //better in this cause you can be sure that your context will stay the
  93.         //same.
  94.     };/* Unnecessary semicolon (functions doesn't need them).
  95.          Use JSHint. */
  96.  
  97.     function error(callback) {
  98.         callbacks.error.push(callback);
  99.         return this; //After first setInterval this == window.
  100.         //Also, it's not used anywhere so returning it is pretty useless.
  101.         //I know that it should work during init only, but prototypes does it
  102.         //better in this cause you can be sure that your context will stay the
  103.         //same.
  104.     };/* Unnecessary semicolon (functions doesn't need them).
  105.          Use JSHint. */
  106.  
  107.     function retry(callback) {
  108.         callbacks.retry.push(callback);
  109.         return this; //After first setInterval this == window.
  110.         //Also, it's not used anywhere so returning it is pretty useless.
  111.         //I know that it should work during init only, but prototypes does it
  112.         //better in this cause you can be sure that your context will stay the
  113.         //same.
  114.     };/* Unnecessary semicolon (functions doesn't need them).
  115.          Use JSHint. */
  116.  
  117.     function start(timeout) {
  118.         // As lower - if(timer) will do exactly the same.
  119.         if(isStarted()) {
  120.             stop();
  121.         }
  122.         resolve();
  123.         timer = setInterval(resolve, timeout);
  124.         return this; //After first setInterval this == window.
  125.         //Also, it's not used anywhere so returning it is pretty useless.
  126.         //I know that it should work during init only, but prototypes does it
  127.         //better in this cause you can be sure that your context will stay the
  128.         //same.
  129.     };/* Unnecessary semicolon (functions doesn't need them).
  130.          Use JSHint. */
  131.  
  132.     function stop() {
  133.         if(timer) {
  134.             clearInterval(timer);
  135.             timer = undefined;
  136.         }
  137.         return this; //After first setInterval this == window.
  138.         //Also, it's not used anywhere so returning it is pretty useless.
  139.         //I know that it should work during init only, but prototypes does it
  140.         //better in this cause you can be sure that your context will stay the
  141.         //same.
  142.     };/* Unnecessary semicolon (functions doesn't need them).
  143.          Use JSHint. */
  144.  
  145.     //Unnecessary function - if you're just checking for truethiness
  146.     //just do if (timer) instead of isStarted()
  147.     function isStarted() {
  148.         return (timer) ? true : false;
  149.     }
  150.  
  151.     return {
  152.         onOpen : onOpen,
  153.         onClosed : onClosed,
  154.         error : error,
  155.         retry : retry,
  156.         start : start,
  157.         stop : stop,
  158.         check : resolve
  159.     };
  160.  
  161. });
  162.  
  163. ^ review całości.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement