Improved memory usage.
This commit is contained in:
parent
6b81b791ed
commit
0d377d40ab
1 changed files with 45 additions and 34 deletions
|
@ -40,15 +40,15 @@ class _ajaxSingleton
|
||||||
cache: false
|
cache: false
|
||||||
async: true
|
async: true
|
||||||
|
|
||||||
current_request: {}
|
currentRequest: {}
|
||||||
queue_list: []
|
queueList: []
|
||||||
queue_running: false
|
queueRunning: false
|
||||||
count: 0
|
count: 0
|
||||||
|
|
||||||
constructor: (@args) ->
|
constructor: (@args) ->
|
||||||
|
|
||||||
# run queue
|
# run queue
|
||||||
@_run()
|
@runNextInQueue()
|
||||||
|
|
||||||
# bindings
|
# bindings
|
||||||
$(document).bind('ajaxSend', =>
|
$(document).bind('ajaxSend', =>
|
||||||
|
@ -80,8 +80,8 @@ class _ajaxSingleton
|
||||||
|
|
||||||
# show error message
|
# show error message
|
||||||
new App.ControllerModal(
|
new App.ControllerModal(
|
||||||
head: 'StatusCode: ' + status
|
head: "StatusCode: #{status}"
|
||||||
contentInline: '<pre>' + App.Utils.htmlEscape(detail) + '</pre>'
|
contentInline: "<pre>#{App.Utils.htmlEscape(detail)}</pre>"
|
||||||
buttonClose: true
|
buttonClose: true
|
||||||
buttonSubmit: false
|
buttonSubmit: false
|
||||||
)
|
)
|
||||||
|
@ -93,55 +93,66 @@ class _ajaxSingleton
|
||||||
# execute call with id, clear old call first if exists
|
# execute call with id, clear old call first if exists
|
||||||
if params['id']
|
if params['id']
|
||||||
@abort(params['id'])
|
@abort(params['id'])
|
||||||
@current_request[ params['id'] ] = $.ajax( data )
|
@addCurrentRequest(params['id'], data)
|
||||||
return params['id']
|
return params['id']
|
||||||
|
|
||||||
# generate a uniq rand id
|
# generate a uniq rand id
|
||||||
params['id'] = 'rand-' + new Date().getTime() + '-' + Math.floor( Math.random() * 99999 )
|
params['id'] = "rand-#{new Date().getTime()}-#{Math.floor(Math.random() * 99999)}"
|
||||||
|
|
||||||
# queue request
|
# queue request
|
||||||
if params['queue']
|
if params['queue']
|
||||||
@queue_list.push data
|
@queueList.push data
|
||||||
if !@queue_running
|
if !@queueRunning
|
||||||
@_run()
|
@runNextInQueue()
|
||||||
|
|
||||||
# execute request
|
# execute request
|
||||||
else
|
else
|
||||||
@current_request[ params['id'] ] = $.ajax(data)
|
@addCurrentRequest(params['id'], data)
|
||||||
|
|
||||||
params['id']
|
params['id']
|
||||||
|
|
||||||
|
addCurrentRequest: (id, data, queueRunning) =>
|
||||||
|
data.complete = =>
|
||||||
|
if queueRunning
|
||||||
|
@queueRunning = false
|
||||||
|
@removeCurrentRequest(id)
|
||||||
|
if queueRunning
|
||||||
|
@runNextInQueue()
|
||||||
|
@currentRequest[id] = $.ajax(data)
|
||||||
|
return if data.async is true
|
||||||
|
@removeCurrentRequest(id)
|
||||||
|
|
||||||
|
removeCurrentRequest: (id) =>
|
||||||
|
@currentRequest[id] = undefined
|
||||||
|
delete @currentRequest[id]
|
||||||
|
|
||||||
abort: (id) =>
|
abort: (id) =>
|
||||||
|
|
||||||
# abort current_request
|
# abort currentRequest
|
||||||
if @current_request[ id ]
|
if @currentRequest[id]
|
||||||
@current_request[ id ].abort()
|
@currentRequest[id].abort()
|
||||||
delete @current_request[ id ]
|
@currentRequest[id] = undefined
|
||||||
|
delete @currentRequest[id]
|
||||||
|
|
||||||
# remove from queue list
|
# remove from queue list
|
||||||
@queue_list = _.filter(
|
@queueList = _.filter(
|
||||||
@queue_list
|
@queueList
|
||||||
(item) ->
|
(item) ->
|
||||||
return item if item['id'] isnt id
|
return item if item['id'] isnt id
|
||||||
return
|
return
|
||||||
)
|
)
|
||||||
|
|
||||||
abortAll: =>
|
abortAll: =>
|
||||||
return if !@current_request
|
|
||||||
abortedIds = []
|
abortedIds = []
|
||||||
for id, ajax of @current_request
|
for id, ajax of @currentRequest
|
||||||
@abort(id)
|
@abort(id)
|
||||||
abortedIds.push id
|
abortedIds.push id
|
||||||
abortedIds
|
abortedIds
|
||||||
|
|
||||||
_run: =>
|
runNextInQueue: =>
|
||||||
if @queue_list && @queue_list[0]
|
if @queueList && @queueList[0]
|
||||||
@queue_running = true
|
@queueRunning = true
|
||||||
request = @queue_list.shift()
|
data = @queueList.shift()
|
||||||
request.complete = =>
|
@addCurrentRequest(params['id'], data, true)
|
||||||
@queue_running = false
|
|
||||||
@_run()
|
|
||||||
@current_request[ request['id'] ] = $.ajax( request )
|
|
||||||
|
|
||||||
_show_spinner: =>
|
_show_spinner: =>
|
||||||
@count++
|
@count++
|
||||||
|
|
Loading…
Reference in a new issue