Created
June 9, 2013 14:58
-
-
Save piroman-lynx/5743836 to your computer and use it in GitHub Desktop.
Умный диспатчер для js.
Позволяет отвызвать бакенд и от форнтенда (бакенд вызывает событие, фронтенд его обрабатывает, если хочет).
Так же можно отвызвывать разные части кода от фронтенда.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var callback = { | |
list : {}, | |
add_callback : function(event, id, func){ | |
if (typeof callback.list[event] == "undefined"){ | |
callback.list[event]={}; | |
} | |
callback.list[event][id]=func; | |
}, | |
remove_callback : function(event, id){ | |
if (typeof callback.list[event] == "undefined"){ | |
return false; | |
} | |
callback.list[event][id]=null; | |
}, | |
list_callbacks : function(event) { | |
return callback.list[event]; | |
}, | |
run_callback : function(event, param){ | |
if (typeof callback.list[event] == "undefined"){ | |
return false; | |
} | |
$.each(callback.list[event],function(k,v){ | |
try { | |
if (v != null){ | |
setTimeout('callback.list["'+event+'"]["'+k+'"](\''+param+'\');',0); | |
} | |
}catch(e){ | |
alert('fail on event '+event+' on run id'+k); | |
} | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
callback.add_callback('dialog_close', 'onClose1', function(){ | |
//do something after close dialog | |
}); | |
callback.add_callback('dialog_close', 'onCloseBad', function(){ | |
//выполняется 1 раз раз загрузку страницы.... | |
//... | |
//... | |
callback.remove_callback('dialog_close', 'onCloseBad'); | |
}); | |
callback.run_callback('dialog_close', ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment