Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Should be a module, not a var. Example:
- //;(function($){})(jQuery)
- var HSIndicator = (function(url){
- var callbacks = {
- onOpen : [],
- onClosed : [],
- error : [],
- retry : []
- },
- versionConverters = {
- '0.8' : function(data) {
- return { state : { open : data.open,
- lastchange : data.lastchange,
- icon : { open : undefined, closed : undefined },
- message : data.status,
- trigger_person : undefined}};
- },
- '0.9' : function(data) {
- return { state : { open : data.open,
- lastchange : data.lastchange,
- icon : { open : undefined, closed : undefined },
- message : data.status,
- trigger_person : undefined}};
- },
- '0.11' : function(data) {
- return { state : { open : data.open,
- lastchange : data.lastchange,
- icon : data.icon,
- message : data.status,
- trigger_person : undefined}};
- },
- '0.12' : function(data) {
- return { state : { open : data.open,
- lastchange : data.lastchange,
- icon : data.icon,
- message : data.status,
- trigger_person : undefined}};
- },
- '0.13' : function(data) {
- return data;
- //missing comma
- }}/* HERE */
- //it's enough to just declare variable
- //declared variable but not set as anything will STILL be
- //an undefined one. Don't ask me how, it just works like that.
- timer = undefined;
- function resolve() {
- //IMHO jQuery is an overkill when using JUST XMLHttpRequest :/
- jQuery.ajax({
- type : 'GET',
- url : url,
- beforeSend : function() {
- callbacks.retry.forEach(function(what) { what(); });
- }
- }).done(function(result) {
- data = versionConverters[result.api](result);
- if(data.state.open) {
- callbacks.onOpen.forEach(function(what) { what(data); });
- }
- else {
- callbacks.onClosed.forEach(function(what) { what(data); });
- }
- }).fail(function(jqXHR, errText, err) {
- callbacks.error.forEach(function(what) { what(errText, err); });
- });
- return this; //After first setInterval this == window.
- //Also, it's not used anywhere so returning it is pretty useless.
- //I know that it should work during init only, but prototypes does it
- //better in this cause you can be sure that your context will stay the
- //same.
- };/* Unnecessary semicolon (functions doesn't need them).
- Use JSHint. */
- function onOpen(callback) {
- callbacks.onOpen.push(callback);
- return this; //After first setInterval this == window.
- //Also, it's not used anywhere so returning it is pretty useless.
- //I know that it should work during init only, but prototypes does it
- //better in this cause you can be sure that your context will stay the
- //same.
- };/* Unnecessary semicolon (functions doesn't need them).
- Use JSHint. */
- function onClosed(callback) {
- callbacks.onClosed.push(callback);
- return this; //After first setInterval this == window.
- //Also, it's not used anywhere so returning it is pretty useless.
- //I know that it should work during init only, but prototypes does it
- //better in this cause you can be sure that your context will stay the
- //same.
- };/* Unnecessary semicolon (functions doesn't need them).
- Use JSHint. */
- function error(callback) {
- callbacks.error.push(callback);
- return this; //After first setInterval this == window.
- //Also, it's not used anywhere so returning it is pretty useless.
- //I know that it should work during init only, but prototypes does it
- //better in this cause you can be sure that your context will stay the
- //same.
- };/* Unnecessary semicolon (functions doesn't need them).
- Use JSHint. */
- function retry(callback) {
- callbacks.retry.push(callback);
- return this; //After first setInterval this == window.
- //Also, it's not used anywhere so returning it is pretty useless.
- //I know that it should work during init only, but prototypes does it
- //better in this cause you can be sure that your context will stay the
- //same.
- };/* Unnecessary semicolon (functions doesn't need them).
- Use JSHint. */
- function start(timeout) {
- // As lower - if(timer) will do exactly the same.
- if(isStarted()) {
- stop();
- }
- resolve();
- timer = setInterval(resolve, timeout);
- return this; //After first setInterval this == window.
- //Also, it's not used anywhere so returning it is pretty useless.
- //I know that it should work during init only, but prototypes does it
- //better in this cause you can be sure that your context will stay the
- //same.
- };/* Unnecessary semicolon (functions doesn't need them).
- Use JSHint. */
- function stop() {
- if(timer) {
- clearInterval(timer);
- timer = undefined;
- }
- return this; //After first setInterval this == window.
- //Also, it's not used anywhere so returning it is pretty useless.
- //I know that it should work during init only, but prototypes does it
- //better in this cause you can be sure that your context will stay the
- //same.
- };/* Unnecessary semicolon (functions doesn't need them).
- Use JSHint. */
- //Unnecessary function - if you're just checking for truethiness
- //just do if (timer) instead of isStarted()
- function isStarted() {
- return (timer) ? true : false;
- }
- return {
- onOpen : onOpen,
- onClosed : onClosed,
- error : error,
- retry : retry,
- start : start,
- stop : stop,
- check : resolve
- };
- });
- ^ review całości.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement