Created
October 20, 2017 09:17
-
-
Save isuke/ad52b2f2f4a63049c2bcaf23a86b6520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # require: $@store | |
| # require: @path | |
| # require: @resourceId | |
| # option: @goToPath | |
| export default | |
| computed: | |
| postData: -> | |
| formObj = new FormData() | |
| _.forEach @$store.state.form, (value, key) => | |
| @_postData(formObj, _.snakeCase(key), value) | |
| formObj | |
| methods: | |
| isFile: (obj) -> Object.prototype.toString.call(obj) == '[object File]' | |
| post: (e) -> | |
| e.target.disabled = true | |
| @$store.dispatch('CLEAR_ERRORS') | |
| axios.post(@path, @postData) | |
| .then (responce) => | |
| toastr.success responce.data.message | |
| @resourceId = responce.data.resource.id | |
| @$store.dispatch('CLEAR_FORM') | |
| @$store.dispatch('OFF_FORM_CHANGED') | |
| window.location.href = @goToPath if @goToPath? | |
| e.target.disabled = false | |
| .catch (responce) => | |
| toastr.error responce.data.message | |
| @$store.dispatch('SET_ERRORS', responce.data.errors) | |
| @$store.dispatch('ON_FORM_CHANGED') | |
| e.target.disabled = false | |
| patch: (e) -> | |
| e.target.disabled = true | |
| @$store.dispatch('CLEAR_ERRORS') | |
| axios.patch(@path, @postData) | |
| .then (responce) => | |
| toastr.success responce.data.message | |
| @$store.dispatch('OFF_FORM_CHANGED') | |
| e.target.disabled = false | |
| .catch (responce) => | |
| toastr.error responce.data.message | |
| @$store.dispatch('SET_ERRORS', responce.data.errors) | |
| @$store.dispatch('ON_FORM_CHANGED') | |
| e.target.disabled = false | |
| _postData: (formObj, paramKeyName, value) -> | |
| _.forEach value, (v, k) => | |
| if _.isArray(v) | |
| _.forEach v, (a, i) => | |
| @_postData(formObj, "#{paramKeyName}[#{_.snakeCase(k)}_attributes][#{i}]", a) | |
| else if _.isObject(v) and !@isFile(v) | |
| @_postData(formObj, "#{paramKeyName}[#{_.snakeCase(k)}_attributes]", v) | |
| else | |
| formObj.append("#{paramKeyName}[#{_.snakeCase(k)}]", v) if v? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment