You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB

  1. (function($K)
  2. {
  3. $K.add('module', 'magicquery', {
  4. init: function(app, context)
  5. {
  6. this.app = app;
  7. this.response = app.response;
  8. // defaults
  9. var defaults = {
  10. url: false
  11. };
  12. // context
  13. this.context = context;
  14. this.params = context.getParams(defaults);
  15. this.$element = context.getElement();
  16. },
  17. // public
  18. start: function()
  19. {
  20. this.$element.on('click.kube.magicquery', this._send.bind(this));
  21. },
  22. stop: function()
  23. {
  24. this._enable();
  25. this.$element.off('.kube.magicquery');
  26. },
  27. // private
  28. _disable: function()
  29. {
  30. this.$element.attr('disabled', true);
  31. },
  32. _enable: function()
  33. {
  34. this.$element.removeAttr('disabled');
  35. },
  36. _send: function(e)
  37. {
  38. e.preventDefault();
  39. this._disable();
  40. $K.ajax.post({
  41. url: this.params.url,
  42. success: this._parse.bind(this)
  43. });
  44. },
  45. _parse: function(data)
  46. {
  47. this._enable();
  48. var json = this.response.parse(data);
  49. if (json)
  50. {
  51. this.app.broadcast('magicquery.success', this, json);
  52. }
  53. },
  54. });
  55. })(Kube);