Implement experimental loading state functionality to prevent double action triggering for saving records
This commit is contained in:
@@ -4,6 +4,7 @@ export default Ember.Component.extend
|
|||||||
modal_options: {}
|
modal_options: {}
|
||||||
layoutName: 'modals/layout'
|
layoutName: 'modals/layout'
|
||||||
willOpenModal: -> # can be implemented, is fired after loading attributes, before rendering
|
willOpenModal: -> # can be implemented, is fired after loading attributes, before rendering
|
||||||
|
is_saving: false # temporary state to prevent double triggering
|
||||||
title: (->
|
title: (->
|
||||||
# return title if directly set by options
|
# return title if directly set by options
|
||||||
return @get('modal_options.title') if @get('modal_options.title')
|
return @get('modal_options.title') if @get('modal_options.title')
|
||||||
@@ -37,14 +38,18 @@ export default Ember.Component.extend
|
|||||||
body: (->
|
body: (->
|
||||||
@get('modal_options.body')
|
@get('modal_options.body')
|
||||||
).property('modal_options.body')
|
).property('modal_options.body')
|
||||||
|
|
||||||
save_error: (error)->
|
save_error: (error)->
|
||||||
|
@set 'is_saving', false
|
||||||
if typeof error is 'string'
|
if typeof error is 'string'
|
||||||
@set 'alert_message', error
|
@set 'alert_message', error
|
||||||
else if error?.status is 403 or error?.status is 401
|
else if error?.status is 403 or error?.status is 401
|
||||||
@set 'alert_message', 'Unauthorized action'
|
@set 'alert_message', 'Unauthorized action'
|
||||||
else
|
else
|
||||||
@set 'alert_message', 'Something went wrong'
|
@set 'alert_message', 'Something went wrong'
|
||||||
|
|
||||||
save_success: ->
|
save_success: ->
|
||||||
|
@set 'is_saving', false
|
||||||
@set 'alert_message', ''
|
@set 'alert_message', ''
|
||||||
pre_hook_modals_count = @get('globals.modals_counter')
|
pre_hook_modals_count = @get('globals.modals_counter')
|
||||||
if save_callback = @get('modal_options.save')
|
if save_callback = @get('modal_options.save')
|
||||||
@@ -74,6 +79,8 @@ export default Ember.Component.extend
|
|||||||
@send 'closeModal' unless @preventClose
|
@send 'closeModal' unless @preventClose
|
||||||
confirm: -> @send('ok')
|
confirm: -> @send('ok')
|
||||||
saveRecord: ->
|
saveRecord: ->
|
||||||
|
return if @get('is_saving')
|
||||||
|
@set 'is_saving', true # prevent click enthusiasts from creating to many instances or things like that
|
||||||
model = @get("model")
|
model = @get("model")
|
||||||
if typeof model.validate is 'function'
|
if typeof model.validate is 'function'
|
||||||
model.validate() # validate if method exists
|
model.validate() # validate if method exists
|
||||||
@@ -81,13 +88,17 @@ export default Ember.Component.extend
|
|||||||
changeset.validate()
|
changeset.validate()
|
||||||
if changeset.get('isValid')
|
if changeset.get('isValid')
|
||||||
changeset.save().then @save_success.bind(@), @save_error.bind(@)
|
changeset.save().then @save_success.bind(@), @save_error.bind(@)
|
||||||
|
else
|
||||||
|
@set 'is_saving', false
|
||||||
else
|
else
|
||||||
if model.get('isValid')
|
if model.get('isValid')
|
||||||
model.save().then @save_success.bind(@), @save_error.bind(@)
|
model.save().then @save_success.bind(@), @save_error.bind(@)
|
||||||
#@send 'closeModal' unless @preventClose
|
#@send 'closeModal' unless @preventClose
|
||||||
else
|
else
|
||||||
console.log "[VALIDATION ERRORS]"
|
console.log "[VALIDATION ERRORS]"
|
||||||
|
@set 'is_saving', false
|
||||||
console.log model.get("errors")
|
console.log model.get("errors")
|
||||||
|
|
||||||
rollback_and_close: ->
|
rollback_and_close: ->
|
||||||
if model = @get("model")
|
if model = @get("model")
|
||||||
if model.__changeset__
|
if model.__changeset__
|
||||||
|
|||||||
Reference in New Issue
Block a user