custom.js.es6 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. export default {
  2. name: 'custom-routes',
  3. initialize(container, app) {
  4. if (app.get('rootElement') !== '#custom-wizard-main') return;
  5. const WizardApplicationRoute = requirejs('wizard/routes/application').default;
  6. const findCustomWizard = requirejs('discourse/plugins/discourse-custom-wizard/wizard/models/custom').findCustomWizard;
  7. const Router = requirejs('wizard/router').default;
  8. const ajax = requirejs('wizard/lib/ajax').ajax;
  9. const StepRoute = requirejs('wizard/routes/step').default;
  10. const StepModel = requirejs('wizard/models/step').default;
  11. Router.map(function() {
  12. this.route('custom', { path: '/custom/:id' }, function() {
  13. this.route('step', { path: '/steps/:step_id' });
  14. });
  15. });
  16. WizardApplicationRoute.reopen({
  17. model() {
  18. const customParams = this.paramsFor('custom');
  19. return findCustomWizard(customParams.id);
  20. },
  21. afterModel(model) {
  22. return ajax({
  23. url: `/site/basic-info`,
  24. type: 'GET',
  25. }).then((result) => {
  26. return model.set('siteInfo', result);
  27. });
  28. },
  29. setupController(controller, model) {
  30. controller.setProperties({
  31. customWizard: true,
  32. siteInfo: model.get('siteInfo')
  33. });
  34. }
  35. });
  36. StepModel.reopen({
  37. save() {
  38. const fields = {};
  39. this.get('fields').forEach(f => fields[f.id] = f.value);
  40. return ajax({
  41. url: `/wizard/custom/${this.get('wizardId')}/steps/${this.get('id')}`,
  42. type: 'PUT',
  43. data: { fields }
  44. }).catch(response => {
  45. response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description));
  46. throw response;
  47. });
  48. }
  49. });
  50. StepRoute.reopen({
  51. afterModel(model) {
  52. const wizard = this.modelFor('application');
  53. return model.set("wizardId", wizard.id);
  54. }
  55. });
  56. }
  57. };