|
1 | 1 | AutoForm.hooks({ |
2 | 2 | apiBackends: { |
3 | | - onSuccess: function (formType, backendId) { |
4 | | - // Send the API Backend to API Umbrella |
5 | | - Meteor.call('createApiBackendOnApiUmbrella', backendId); |
6 | | - // Redirect to the just created API Backend page |
7 | | - Router.go('viewApiBackend', {_id: backendId}); |
| 3 | + beginSubmit: function () { |
| 4 | + // Disable form elements while submitting form |
| 5 | + $('[data-schema-key], button').attr("disabled", "disabled"); |
| 6 | + }, |
| 7 | + endSubmit: function () { |
| 8 | + // Enable form elements after form submission |
| 9 | + $('[data-schema-key], button').removeAttr("disabled"); |
| 10 | + }, |
| 11 | + before: { |
| 12 | + insert: function (apiBackendForm) { |
| 13 | + // Keep the context to use inside the callback function |
| 14 | + var context = this; |
| 15 | + |
| 16 | + // Send the API Backend to API Umbrella |
| 17 | + response = Meteor.call('createApiBackendOnApiUmbrella', apiBackendForm, function(error, apiUmbrellaWebResponse) { |
| 18 | + |
| 19 | + //apiUmbrellaWebResponse contents |
| 20 | + // apiUmbrellaWebResponse = { |
| 21 | + // result: {}, |
| 22 | + // http_status: 200, |
| 23 | + // errors: {} |
| 24 | + // }; |
| 25 | + |
| 26 | + if (apiUmbrellaWebResponse.http_status === 200) { |
| 27 | + // Submit form on meteor:api-umbrella success |
| 28 | + context.result(apiBackendForm); |
| 29 | + } else { |
| 30 | + // Error data structure returned. |
| 31 | + // nowadays, jerry-rig solution: |
| 32 | + // {"default":'{"backend_protocol":["is not included in the list"]}}' |
| 33 | + // after https://github.com/brylie/meteor-api-umbrella/issues/1 is resolved, it should be: |
| 34 | + // {"frontend_host":["must be in the format of \"example.com\""], |
| 35 | + // "backend_host":["must be in the format of \"example.com\""], |
| 36 | + // "base":["must have at least one url_matches"], |
| 37 | + // "servers[0].host":["must be in the format of \"example.com\"","Could not resolve host: no address for http://api.example.com"], |
| 38 | + // "servers[0].port":["can't be blank","is not included in the list"]} |
| 39 | + var errors = _.values(apiUmbrellaWebResponse.errors); |
| 40 | + |
| 41 | + // Flatten all error descriptions to show using sAlert |
| 42 | + errors = _.flatten(errors); |
| 43 | + _.each(errors, function(error) { |
| 44 | + //Display error to the user, keep the sAlert box visible. |
| 45 | + sAlert.error(error, {timeout: 'none'}); |
| 46 | + // TODO: Figure out a way to send the errors back to the autoform fields, as if it were client validation, |
| 47 | + // and get rid of sAlert here. |
| 48 | + }); |
| 49 | + |
| 50 | + //Cancel form submission on error, so user see the sAlert.error message and edit the incorrect fields |
| 51 | + context.result(false); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + }, |
| 56 | + onSuccess: function (formType, apiBackendId) { |
| 57 | + //Redirect to the just created API Backend page |
| 58 | + Router.go('viewApiBackend', {_id: apiBackendId}); |
8 | 59 | } |
9 | 60 | } |
10 | 61 | }); |
0 commit comments