From 8db7b51c329b863606e7185136ef02ac9675323c Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 3 Apr 2014 15:25:33 +0200 Subject: [PATCH] Improved userTicketPopups feature. --- .../_application_controller.js.coffee | 95 +++++++++++-------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index be39821f9..860dc7957 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -385,51 +385,66 @@ class App.Controller extends Spine.Controller ) ) - userTicketPopups: (data) -> + userTicketPopups: (params) -> # remove old popovers $('.popover').remove() + show = (data, tickets) => + + if !data.position + data.position = 'left' + + # show user popup + controller = @ + @el.find(data.selector).popover( + trigger: 'hover' + container: 'body' + html: true + delay: { show: 500, hide: 5200 } + placement: data.position + title: -> + $(@).find('[title="*"]').val() + + content: -> + type = $(@).filter('[data-type]').data('type') + data = tickets[type] || [] + + # set human time + for ticket in data + ticket.humanTime = controller.humanTime(ticket.created_at) + + # insert data + App.view('popover/user_ticket_list')( + tickets: data, + ) + ) + + fetch = (params) => + @ajax( + type: 'GET', + url: @Config.get('api_path') + '/ticket_customer', + data: { + customer_id: params.user_id, + } + processData: true, + success: (data, status, xhr) => + App.Store.write( "user-ticket-popover::#{params.user_id}", data.tickets ) + show( params, data.tickets ) + ) + # get data - tickets = {} - App.Ajax.request( - type: 'GET', - url: @Config.get('api_path') + '/ticket_customer', - data: { - customer_id: data.user_id, - } - processData: true, - success: (data, status, xhr) => - tickets = data.tickets - ) - - if !data.position - data.position = 'left' - - # show user popup - controller = @ - @el.find(data.selector).popover( - trigger: 'hover' - container: 'body' - html: true - delay: { show: 500, hide: 5200 } - placement: data.position - title: -> - $(@).find('[title="*"]').val() - - content: -> - type = $(@).filter('[data-type]').data('type') - data = tickets[type] || [] - - # set human time - for ticket in data - ticket.humanTime = controller.humanTime(ticket.created_at) - - # insert data - App.view('popover/user_ticket_list')( - tickets: data, - ) - ) + tickets = App.Store.get( "user-ticket-popover::#{params.user_id}" ) + if tickets + show( params, tickets ) + @delay( + => + fetch(params) + 1000 + 'fetch' + ) + else + fetch(params) ws_send: (data) -> App.Event.trigger( 'ws:send', JSON.stringify(data) )