form.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const Mn = require('backbone.marionette');
  2. const App = require('../../main');
  3. const SslPassthroughModel = require('../../../models/ssl-passthrough-host');
  4. const template = require('./form.ejs');
  5. require('jquery-serializejson');
  6. require('jquery-mask-plugin');
  7. require('selectize');
  8. module.exports = Mn.View.extend({
  9. template: template,
  10. className: 'modal-dialog',
  11. ui: {
  12. form: 'form',
  13. forwarding_host: 'input[name="forwarding_host"]',
  14. buttons: '.modal-footer button',
  15. cancel: 'button.cancel',
  16. save: 'button.save'
  17. },
  18. events: {
  19. 'change @ui.switches': function () {
  20. this.ui.type_error.hide();
  21. },
  22. 'click @ui.save': function (e) {
  23. e.preventDefault();
  24. if (!this.ui.form[0].checkValidity()) {
  25. $('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
  26. return;
  27. }
  28. let view = this;
  29. let data = this.ui.form.serializeJSON();
  30. // Manipulate
  31. data.forwarding_port = parseInt(data.forwarding_port, 10);
  32. let method = App.Api.Nginx.SslPassthroughHosts.create;
  33. let is_new = true;
  34. if (this.model.get('id')) {
  35. // edit
  36. is_new = false;
  37. method = App.Api.Nginx.SslPassthroughHosts.update;
  38. data.id = this.model.get('id');
  39. }
  40. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  41. method(data)
  42. .then(result => {
  43. view.model.set(result);
  44. App.UI.closeModal(function () {
  45. if (is_new) {
  46. App.Controller.showNginxSslPassthrough();
  47. }
  48. });
  49. })
  50. .catch(err => {
  51. alert(err.message);
  52. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  53. });
  54. }
  55. },
  56. initialize: function (options) {
  57. if (typeof options.model === 'undefined' || !options.model) {
  58. this.model = new SslPassthroughModel.Model();
  59. }
  60. }
  61. });