misago-uiserver.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Misago UI server
  2. var MisagoUIServer = function(frequency) {
  3. if (frequency == undefined) {
  4. frequency = 15000;
  5. }
  6. this.frequency = frequency;
  7. var _this = this;
  8. this.listeners = [];
  9. this.on_data = function(name, callback) {
  10. if (callback == undefined) {
  11. this.listeners.push({callback: name});
  12. } else {
  13. this.listeners.push({name: name, callback: callback});
  14. }
  15. };
  16. this.update = function() {
  17. $.get(uiserver_url, function(data) {
  18. $.each(_this.listeners, function(i, listener) {
  19. if (listener.name == undefined) {
  20. listener.callback(data);
  21. } else if (typeof data[listener.name] !== "undefined") {
  22. listener.callback(data[listener.name]);
  23. }
  24. });
  25. Misago.DOM.changed();
  26. window.setTimeout(function() {
  27. _this.update();
  28. }, _this.frequency);
  29. });
  30. };
  31. }
  32. // Create server and start it 2 seconds after dom ready
  33. Misago.Server = new MisagoUIServer();
  34. $(function() {
  35. window.setTimeout(function() {
  36. Misago.Server.update();
  37. }, 2000);
  38. });