action.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. (function (Dcat) {
  2. class Action {
  3. constructor(options) {
  4. this.options = $.extend({
  5. selector: null, // 按钮选择器
  6. event: 'click',
  7. method: 'POST',
  8. key: null, // 行主键
  9. url: null,
  10. data: {}, // 发送到接口的附加参数
  11. confirm: null,
  12. calledClass: null,
  13. before: function (data, target) {}, // 发起请求之前回调,返回false可以中断请求
  14. html: function (target, html, data) { // 处理返回的HTML代码
  15. target.html(html);
  16. },
  17. success: function (target, results) {}, // 请求成功回调,返回false可以中断默认的成功处理逻辑
  18. error: function (target, results) {}, // 请求出错回调,返回false可以中断默认的错误处理逻辑
  19. }, options);
  20. this.init();
  21. }
  22. init() {
  23. let _this = this, options = _this.options;
  24. $(options.selector).off(options.event).on(options.event, function (e) {
  25. let data = $(this).data(),
  26. target = $(this);
  27. if (target.attr('loading') > 0) {
  28. return;
  29. }
  30. if (options.before(data, target, _this) === false) {
  31. return;
  32. }
  33. // 发起请求
  34. function request() {
  35. target.attr('loading', 1);
  36. Object.assign(data, options.data);
  37. _this.promise(target, data).then(_this.resolve()).catch(_this.reject());
  38. }
  39. var conform = options.confirm;
  40. if (conform) {
  41. Dcat.confirm(conform[0], conform[1], request);
  42. } else {
  43. request()
  44. }
  45. });
  46. }
  47. resolve() {
  48. let _this = this, options = _this.options;
  49. return function (result) {
  50. var response = result[0],
  51. target = result[1];
  52. if (options.success(target, response) === false) {
  53. return;
  54. }
  55. Dcat.handleJsonResponse(response, {html: options.html, target: target});
  56. };
  57. }
  58. reject() {
  59. let options = this.options;
  60. return function (result) {
  61. var request = result[0], target = result[1];
  62. if (options.success(target, request) === false) {
  63. return;
  64. }
  65. if (request && typeof request.responseJSON === 'object') {
  66. Dcat.error(request.responseJSON.message)
  67. }
  68. console.error(result);
  69. }
  70. }
  71. promise(target, data) {
  72. let options = this.options;
  73. return new Promise(function (resolve, reject) {
  74. Object.assign(data, {
  75. _action: options.calledClass,
  76. _key: options.key,
  77. });
  78. Dcat.NP.start();
  79. $.ajax({
  80. method: options.method,
  81. url: options.url,
  82. data: data,
  83. success: function (data) {
  84. target.attr('loading', 0);
  85. Dcat.NP.done();
  86. resolve([data, target]);
  87. },
  88. error:function(request){
  89. target.attr('loading', 0);
  90. Dcat.NP.done();
  91. reject([request, target]);
  92. }
  93. });
  94. });
  95. }
  96. }
  97. Dcat.Action = function (opts) {
  98. return new Action(opts);
  99. };
  100. })(Dcat);