mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-04-27 14:33:59 -04:00
CocoViews and CocoModels now notify users of connection errors and automatically retry when getting and putting data goes wrong.
This commit is contained in:
parent
b2b25f354c
commit
a48fa41edd
2 changed files with 14 additions and 0 deletions
app
|
@ -116,6 +116,7 @@ class CocoModel extends Backbone.Model
|
|||
|
||||
save: (attrs, options) ->
|
||||
options ?= {}
|
||||
originalOptions = _.cloneDeep(options)
|
||||
options.headers ?= {}
|
||||
options.headers['X-Current-Path'] = document.location?.pathname ? 'unknown'
|
||||
success = options.success
|
||||
|
@ -128,6 +129,10 @@ class CocoModel extends Backbone.Model
|
|||
CocoModel.pollAchievements()
|
||||
options.success = options.error = null # So the callbacks can be garbage-collected.
|
||||
options.error = (model, res) =>
|
||||
if res.status is 0
|
||||
msg = $.i18n.t 'loading_error.connection_failure', defaultValue: 'Connection failed.'
|
||||
noty text: msg, layout: 'center', type: 'error', killer: true, timeout: 3000
|
||||
return _.delay((f = => @save(attrs, originalOptions)), 3000)
|
||||
error(@, res) if error
|
||||
return unless @notyErrors
|
||||
errorMessage = "Error saving #{@get('name') ? @type()}"
|
||||
|
|
|
@ -49,6 +49,7 @@ module.exports = class CocoView extends Backbone.View
|
|||
@listenTo(@supermodel, 'loaded-all', @onLoaded)
|
||||
@listenTo(@supermodel, 'update-progress', @updateProgress)
|
||||
@listenTo(@supermodel, 'failed', @onResourceLoadFailed)
|
||||
@warnConnectionError = _.throttle(@warnConnectionError, 3000)
|
||||
|
||||
super options
|
||||
|
||||
|
@ -149,6 +150,10 @@ module.exports = class CocoView extends Backbone.View
|
|||
# Error handling for loading
|
||||
onResourceLoadFailed: (e) ->
|
||||
r = e.resource
|
||||
if r.jqxhr?.status is 0
|
||||
@warnConnectionError()
|
||||
return _.delay (=> r.load()), 3000
|
||||
|
||||
@$el.find('.loading-container .errors').append(loadingErrorTemplate({
|
||||
status: r.jqxhr?.status
|
||||
name: r.name
|
||||
|
@ -157,6 +162,10 @@ module.exports = class CocoView extends Backbone.View
|
|||
})).i18n()
|
||||
@$el.find('.progress').hide()
|
||||
|
||||
warnConnectionError: ->
|
||||
msg = $.i18n.t 'loading_error.connection_failure', defaultValue: 'Connection failed.'
|
||||
noty text: msg, layout: 'center', type: 'error', killer: true, timeout: 3000
|
||||
|
||||
onRetryResource: (e) ->
|
||||
res = @supermodel.getResource($(e.target).data('resource-index'))
|
||||
# different views may respond to this call, and not all have the resource to reload
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue