From d4caa3fbfc576b37f17e6cddbcdf71241f60aa75 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 7 Aug 2013 09:45:55 +0200 Subject: [PATCH] Fixed ajax name spacing issue. Added controller based .abort() on remove of controller from DOM (improved memory management). --- .../_application_controller.js.coffee | 12 +++- .../_dashboard/activity_stream.js.coffee | 2 +- .../_dashboard/recent_viewed.js.coffee | 2 +- .../app/controllers/_dashboard/rss.js.coffee | 2 +- .../controllers/_dashboard/ticket.js.coffee | 2 +- .../controllers/_profile/language.js.coffee | 2 +- .../_profile/linked_accounts.js.coffee | 2 +- .../controllers/_profile/password.js.coffee | 2 +- .../controllers/agent_ticket_create.js.coffee | 2 +- .../agent_ticket_history.js.coffee | 2 +- .../controllers/agent_ticket_merge.js.coffee | 4 +- .../customer_ticket_create.js.coffee | 2 +- .../app/controllers/getting_started.js.coffee | 2 +- .../controllers/link_info_widget.js.coffee | 6 +- .../app/controllers/navigation.js.coffee | 4 +- .../app/controllers/package.js.coffee | 4 +- .../app/controllers/reset_password.js.coffee | 6 +- .../app/controllers/session.js.coffee | 6 +- .../app/controllers/tag_widget.js.coffee | 6 +- .../app/controllers/ticket_overview.js.coffee | 4 +- .../app/controllers/ticket_zoom.js.coffee | 2 +- .../app/lib/app_init/track.js.coffee | 2 +- .../app/lib/app_post/ajax.js.coffee | 56 ++++++++++++++----- .../app/lib/app_post/auth.js.coffee | 6 +- .../app/lib/app_post/i18n.js.coffee | 2 +- .../app/lib/app_post/websocket.js.coffee | 6 +- 26 files changed, 93 insertions(+), 55 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index f7ad9a978..3aa434c09 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -23,6 +23,12 @@ class App.Controller extends Spine.Controller # create common accessors @apiPath = @Config.get('api_path') + # remember ajax calls to abort them on dom release + @ajaxCalls = [] + @ajax = (data) => + ajaxId = App.Ajax.request(data) + @ajaxCalls.push ajaxId + bind: (event, callback) => App.Event.bind( event @@ -53,6 +59,9 @@ class App.Controller extends Spine.Controller App.Event.unbindLevel(@controllerId) App.Delay.clearLevel(@controllerId) App.Interval.clearLevel(@controllerId) + if @ajaxCalls + for callId in @ajaxCalls + App.Ajax.abort(callId) release: => # release custom bindings after it got removed from dom @@ -74,7 +83,6 @@ class App.Controller extends Spine.Controller App.Interval.reset() App.WebSocket.close( force: true ) - # add @notify methode to create notification notify: (data) -> App.Event.trigger 'notify', data @@ -376,7 +384,7 @@ class App.Controller extends Spine.Controller # get data tickets = {} - App.Com.ajax( + App.Ajax.request( type: 'GET', url: @Config.get('api_path') + '/ticket_customer', data: { diff --git a/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee index 4e47ae52b..70a74d844 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee @@ -20,7 +20,7 @@ class App.DashboardActivityStream extends App.Controller # init fetch via ajax, all other updates on time via websockets else - App.Com.ajax( + @ajax( id: 'dashoard_activity_stream' type: 'GET' url: @apiPath + '/activity_stream' diff --git a/app/assets/javascripts/app/controllers/_dashboard/recent_viewed.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/recent_viewed.js.coffee index f6106d3bd..56cbde3e5 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/recent_viewed.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/recent_viewed.js.coffee @@ -8,7 +8,7 @@ class App.DashboardRecentViewed extends App.Controller @items = [] # get data - App.Com.ajax( + @ajax( id: 'dashboard_recent_viewed', type: 'GET', url: @apiPath + '/recent_viewed', diff --git a/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee index da2a71d3d..554162483 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee @@ -18,7 +18,7 @@ class App.DashboardRss extends App.Controller # init fetch via ajax, all other updates on time via websockets else - App.Com.ajax( + @ajax( id: 'dashboard_rss' type: 'GET' url: @apiPath + '/rss_fetch' diff --git a/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee index cd575d591..03678756a 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee @@ -27,7 +27,7 @@ class App.DashboardTicket extends App.Controller # init fetch via ajax, all other updates on time via websockets else - App.Com.ajax( + @ajax( id: 'dashboard_ticket_' + @key, type: 'GET', url: @apiPath + '/ticket_overviews', diff --git a/app/assets/javascripts/app/controllers/_profile/language.js.coffee b/app/assets/javascripts/app/controllers/_profile/language.js.coffee index 0423cf4ef..836f2f474 100644 --- a/app/assets/javascripts/app/controllers/_profile/language.js.coffee +++ b/app/assets/javascripts/app/controllers/_profile/language.js.coffee @@ -34,7 +34,7 @@ class App.ProfileLanguage extends App.Controller # get data @locale = params['locale'] - App.Com.ajax( + @ajax( id: 'preferences' type: 'PUT' url: @apiPath + '/users/preferences' diff --git a/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee b/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee index fcacbcda0..9b7447aa5 100644 --- a/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee +++ b/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee @@ -50,7 +50,7 @@ class App.ProfileLinkedAccounts extends App.Controller uid = $(e.target).data('uid') # get data - App.Com.ajax( + @ajax( id: 'account' type: 'DELETE' url: @apiPath + '/users/account' diff --git a/app/assets/javascripts/app/controllers/_profile/password.js.coffee b/app/assets/javascripts/app/controllers/_profile/password.js.coffee index 49b69e2af..38b706c5e 100644 --- a/app/assets/javascripts/app/controllers/_profile/password.js.coffee +++ b/app/assets/javascripts/app/controllers/_profile/password.js.coffee @@ -35,7 +35,7 @@ class App.ProfilePassword extends App.Controller @formDisable(e) # get data - App.Com.ajax( + @ajax( id: 'password_reset' type: 'POST' url: @apiPath + '/users/password_change' diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index 8b49364a3..a8cd95db7 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -102,7 +102,7 @@ class App.TicketCreate extends App.Controller @render() else - App.Com.ajax( + @ajax( id: 'ticket_create' type: 'GET' url: @apiPath + '/ticket_create' diff --git a/app/assets/javascripts/app/controllers/agent_ticket_history.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_history.js.coffee index 0c048a4d0..2c89a6ad8 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_history.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_history.js.coffee @@ -10,7 +10,7 @@ class App.TicketHistory extends App.ControllerModal fetch: (@ticket_id) -> # get data - App.Com.ajax( + @ajax( id: 'ticket_history', type: 'GET', url: @apiPath + '/ticket_history/' + ticket_id, diff --git a/app/assets/javascripts/app/controllers/agent_ticket_merge.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_merge.js.coffee index 4ffb9689d..6daae0830 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_merge.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_merge.js.coffee @@ -6,7 +6,7 @@ class App.TicketMerge extends App.ControllerModal fetch: -> # merge tickets - App.Com.ajax( + @ajax( id: 'ticket_merge_list', type: 'GET', url: @apiPath + '/ticket_merge_list/' + @ticket_id, @@ -104,7 +104,7 @@ class App.TicketMerge extends App.ControllerModal params = @formParam(e.target) # merge tickets - App.Com.ajax( + @ajax( id: 'ticket_merge', type: 'GET', url: @apiPath + '/ticket_merge/' + @ticket_id + '/' + params['master_ticket_number'], diff --git a/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee index 3c37e9b2e..a3af61b45 100644 --- a/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee @@ -34,7 +34,7 @@ class Index extends App.ControllerContent @render() else - App.Com.ajax( + @ajax( id: 'ticket_create', type: 'GET', url: @apiPath + '/ticket_create', diff --git a/app/assets/javascripts/app/controllers/getting_started.js.coffee b/app/assets/javascripts/app/controllers/getting_started.js.coffee index d4e149c19..a20266b72 100644 --- a/app/assets/javascripts/app/controllers/getting_started.js.coffee +++ b/app/assets/javascripts/app/controllers/getting_started.js.coffee @@ -18,7 +18,7 @@ class Index extends App.ControllerContent fetch: -> # get data - App.Com.ajax( + @ajax( id: 'getting_started', type: 'GET', url: @apiPath + '/getting_started', diff --git a/app/assets/javascripts/app/controllers/link_info_widget.js.coffee b/app/assets/javascripts/app/controllers/link_info_widget.js.coffee index 01c803cae..9bf17db04 100644 --- a/app/assets/javascripts/app/controllers/link_info_widget.js.coffee +++ b/app/assets/javascripts/app/controllers/link_info_widget.js.coffee @@ -11,7 +11,7 @@ class App.LinkInfo extends App.Controller fetch: () => # fetch item on demand # get data - App.Com.ajax( + @ajax( id: 'links_' + @object.id + '_' + @object_type, type: 'GET', url: @apiPath + '/links', @@ -80,7 +80,7 @@ class App.LinkInfo extends App.Controller link_object_target_value = @object.id # get data - App.Com.ajax( + @ajax( id: 'links_remove_' + @object.id + '_' + @object_type, type: 'GET', url: @apiPath + '/links/remove', @@ -123,7 +123,7 @@ class App.LinkAdd extends App.ControllerModal params = @formParam(e.target) # get data - App.Com.ajax( + @ajax( id: 'links_add_' + @object.id + '_' + @object_type, type: 'GET', url: @apiPath + '/links/add', diff --git a/app/assets/javascripts/app/controllers/navigation.js.coffee b/app/assets/javascripts/app/controllers/navigation.js.coffee index ba6f261f1..11e5d7a2c 100644 --- a/app/assets/javascripts/app/controllers/navigation.js.coffee +++ b/app/assets/javascripts/app/controllers/navigation.js.coffee @@ -83,8 +83,8 @@ class App.Navigation extends App.Controller @searchFocusSet = false searchFunction = => - App.Com.ajax( - id: 'ticket_search' + App.Ajax.request( + id: 'search' type: 'GET' url: @apiPath + '/search' data: diff --git a/app/assets/javascripts/app/controllers/package.js.coffee b/app/assets/javascripts/app/controllers/package.js.coffee index cda1f6dc8..bd9026aa2 100644 --- a/app/assets/javascripts/app/controllers/package.js.coffee +++ b/app/assets/javascripts/app/controllers/package.js.coffee @@ -13,7 +13,7 @@ class Index extends App.ControllerContent @load() load: -> - App.Com.ajax( + @ajax( id: 'packages', type: 'GET', url: @apiPath + '/packages', @@ -47,7 +47,7 @@ class Index extends App.ControllerContent httpType = 'DELETE' if httpType - App.Com.ajax( + @ajax( id: 'packages', type: httpType, url: @apiPath + '/packages', diff --git a/app/assets/javascripts/app/controllers/reset_password.js.coffee b/app/assets/javascripts/app/controllers/reset_password.js.coffee index c154d2f11..e65b12ad3 100644 --- a/app/assets/javascripts/app/controllers/reset_password.js.coffee +++ b/app/assets/javascripts/app/controllers/reset_password.js.coffee @@ -43,7 +43,7 @@ class Index extends App.ControllerContent @formDisable(e) # get data - App.Com.ajax( + @ajax( id: 'password_reset' type: 'POST' url: @apiPath + '/users/password_reset' @@ -83,7 +83,7 @@ class Verify extends App.ControllerContent # get data params = {} params['token'] = @token - App.Com.ajax( + @ajax( id: 'password_reset_verify' type: 'POST' url: @apiPath + '/users/password_reset_verify' @@ -119,7 +119,7 @@ class Verify extends App.ControllerContent @password = params['password'] # get data - App.Com.ajax( + @ajax( id: 'password_reset_verify' type: 'POST' url: @apiPath + '/users/password_reset_verify' diff --git a/app/assets/javascripts/app/controllers/session.js.coffee b/app/assets/javascripts/app/controllers/session.js.coffee index 0918f3f10..40258c131 100644 --- a/app/assets/javascripts/app/controllers/session.js.coffee +++ b/app/assets/javascripts/app/controllers/session.js.coffee @@ -11,12 +11,12 @@ class Session extends App.ControllerContent @interval( => @load() - 10000 + 30000 ) # fetch data, render view load: -> - App.Com.ajax( + @ajax( id: 'sessions' type: 'GET' url: @apiPath + '/sessions' @@ -42,7 +42,7 @@ class Session extends App.ControllerContent destroy: (e) -> e.preventDefault() sessionId = $( e.target ).data('session-id') - App.Com.ajax( + @ajax( id: 'sessions/' + sessionId type: 'DELETE' url: @apiPath + '/sessions/' + sessionId diff --git a/app/assets/javascripts/app/controllers/tag_widget.js.coffee b/app/assets/javascripts/app/controllers/tag_widget.js.coffee index 2d125f18c..f6b1a2075 100644 --- a/app/assets/javascripts/app/controllers/tag_widget.js.coffee +++ b/app/assets/javascripts/app/controllers/tag_widget.js.coffee @@ -9,7 +9,7 @@ class App.TagWidget extends App.Controller load: => @attribute_id = 'tags_' + @object.id + '_' + @object_type - App.Com.ajax( + @ajax( id: @attribute_id type: 'GET' url: @apiPath + '/tags' @@ -40,7 +40,7 @@ class App.TagWidget extends App.Controller # @el.find('#tags').elastic() onAddTag: (item) => - App.Com.ajax( + @ajax( type: 'GET', url: @apiPath + '/tags/add', data: @@ -53,7 +53,7 @@ class App.TagWidget extends App.Controller ) onRemoveTag: (item) => - App.Com.ajax( + @ajax( type: 'GET' url: @apiPath + '/tags/remove' data: diff --git a/app/assets/javascripts/app/controllers/ticket_overview.js.coffee b/app/assets/javascripts/app/controllers/ticket_overview.js.coffee index 219c7d294..c9ebf5381 100644 --- a/app/assets/javascripts/app/controllers/ticket_overview.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_overview.js.coffee @@ -39,7 +39,7 @@ class Index extends App.ControllerContent # init fetch via ajax, all other updates on time via websockets else - App.Com.ajax( + @ajax( id: 'ticket_overview_' + @key, type: 'GET', url: @apiPath + '/ticket_overviews', @@ -500,7 +500,7 @@ class Router extends App.Controller @ticket_list = cache.ticket_list @redirect() else - App.Com.ajax( + @ajax( type: 'GET' url: @apiPath + '/ticket_overviews' data: diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index 155af5601..eea3c67da 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -64,7 +64,7 @@ class App.TicketZoom extends App.Controller return if !@Session.all() # get data - App.Com.ajax( + @ajax( id: 'ticket_zoom_' + ticket_id type: 'GET' url: @apiPath + '/ticket_full/' + ticket_id + '?do_not_log=' + @doNotLog diff --git a/app/assets/javascripts/app/lib/app_init/track.js.coffee b/app/assets/javascripts/app/lib/app_init/track.js.coffee index a94c1cc92..836b8f622 100644 --- a/app/assets/javascripts/app/lib/app_init/track.js.coffee +++ b/app/assets/javascripts/app/lib/app_init/track.js.coffee @@ -113,7 +113,7 @@ class _trackSingleton catch e # nothing - App.Com.ajax( + App.Ajax.request( type: 'POST' url: @url async: async diff --git a/app/assets/javascripts/app/lib/app_post/ajax.js.coffee b/app/assets/javascripts/app/lib/app_post/ajax.js.coffee index 6a0c8512c..4dc599d99 100644 --- a/app/assets/javascripts/app/lib/app_post/ajax.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/ajax.js.coffee @@ -1,9 +1,14 @@ -class App.Com +class App.Ajax _instance = undefined # Must be declared here to force the closure on the class - @ajax: (args) -> # Must be a static method + @request: (args) -> # Must be a static method if _instance == undefined _instance ?= new _ajaxSingleton - _instance.ajax(args) + _instance.request(args) + + @abort: (args) -> # Must be a static method + if _instance == undefined + _instance ?= new _ajaxSingleton + _instance.abort(args) # The actual Singleton class class _ajaxSingleton @@ -61,19 +66,44 @@ class _ajaxSingleton ) ) - ajax: (params) -> + request: (params) -> data = $.extend({}, @defaults, params ) + + # execute call with id, clear old call first if exists if params['id'] - if @current_request[ params['id'] ] - @current_request[ params['id'] ].abort() + @abort( params['id'] ) @current_request[ params['id'] ] = $.ajax( data ) + return params['id'] + + # generate a uniq rand id + params['id'] = 'rand-' + new Date().getTime() + '-' + Math.floor( Math.random() * 99999 ) + + # queue request + if params['queue'] + @queue_list.push data + if !@queue_running + @_run() + + # execute request else - if params['queue'] - @queue_list.push data - if !@queue_running - @_run() - else - $.ajax(data) + @current_request[ params['id'] ] = $.ajax(data) + + params['id'] + + abort: (id) => + + # abort current_request + if @current_request[ id ] + @current_request[ id ].abort() + delete @current_request[ id ] + + # remove from queue list + @queue_list = _.filter( + @queue_list + (item) -> + return item if item['id'] isnt id + return + ) _run: => if @queue_list && @queue_list[0] @@ -82,7 +112,7 @@ class _ajaxSingleton request.complete = => @queue_running = false @_run() - $.ajax( request ) + @current_request[ request['id'] ] = $.ajax( request ) _show_spinner: => @count++ diff --git a/app/assets/javascripts/app/lib/app_post/auth.js.coffee b/app/assets/javascripts/app/lib/app_post/auth.js.coffee index beb9c7832..cc728c952 100644 --- a/app/assets/javascripts/app/lib/app_post/auth.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/auth.js.coffee @@ -2,7 +2,7 @@ class App.Auth @login: (params) -> App.Log.notice 'Auth', 'login', params - App.Com.ajax( + App.Ajax.request( id: 'login', type: 'POST', url: App.Config.get('api_path') + '/signin', @@ -22,7 +22,7 @@ class App.Auth @loginCheck: -> App.Log.notice 'Auth', 'loginCheck' - App.Com.ajax( + App.Ajax.request( id: 'login_check' async: false type: 'GET' @@ -38,7 +38,7 @@ class App.Auth @logout: -> App.Log.notice 'Auth', 'logout' - App.Com.ajax( + App.Ajax.request( id: 'logout' type: 'DELETE' url: App.Config.get('api_path') + '/signout' diff --git a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee index 8c2a442ff..5f3fe4bc4 100644 --- a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee @@ -100,7 +100,7 @@ class _i18nSingleton extends Spine.Module @locale = locale @map = {} - App.Com.ajax( + App.Ajax.request( id: 'i18n-set-' + locale, type: 'GET', url: App.Config.get('api_path') + '/translations/lang/' + locale, diff --git a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee index e5be0b9ba..ea1d9593e 100644 --- a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee @@ -291,7 +291,7 @@ class _webSocketSingleton extends App.Controller if @_ajaxInitWorking @_ajaxInitWorking.abort() # call init request - @_ajaxInitWorking = App.Com.ajax( + @_ajaxInitWorking = App.Ajax.request( type: 'POST' url: @Config.get('api_path') + '/message_send' data: JSON.stringify({ data: { action: 'login' } }) @@ -327,7 +327,7 @@ class _webSocketSingleton extends App.Controller _ajaxSendQueue: => while !_.isEmpty(@queue) data = @queue.shift() - App.Com.ajax( + App.Ajax.request( type: 'POST' url: @Config.get('api_path') + '/message_send' data: JSON.stringify({ client_id: @client_id, data: data }) @@ -346,7 +346,7 @@ class _webSocketSingleton extends App.Controller return if !@client_id return if @_ajaxReceiveWorking is true @_ajaxReceiveWorking = true - App.Com.ajax( + App.Ajax.request( id: 'message_receive', type: 'POST' url: @Config.get('api_path') + '/message_receive'