Improved AJAX pulling.

This commit is contained in:
Martin Edenhofer 2012-05-08 23:42:23 +02:00
parent 8a1226a708
commit 54d5cab51d
9 changed files with 222 additions and 158 deletions

View file

@ -539,7 +539,29 @@ class App.Controller extends Spine.Controller
return newInstance return newInstance
interval: (action, interval, interval_id) =>
# check global var
if !@intervalID
@intervalID = {}
action()
# auto save
every = (ms, cb) -> setInterval cb, ms
# clear auto save
clearInterval( @intervalID[interval_id] ) if @intervalID[interval_id]
# request new data
@intervalID[interval_id] = every interval, () =>
action()
userPopups: (position = 'right') -> userPopups: (position = 'right') ->
# remove old popovers
$('.popover-inner').parent().remove()
# show user popup # show user popup
$('.user-data').popover( $('.user-data').popover(
delay: { show: 500, hide: 1200 }, delay: { show: 500, hide: 1200 },
@ -570,6 +592,10 @@ class App.Controller extends Spine.Controller
) )
userTicketPopups: (data) -> userTicketPopups: (data) ->
# remove old popovers
$('.popover-inner').parent().remove()
# get data # get data
@tickets = {} @tickets = {}
ajax = new App.Ajax ajax = new App.Ajax

View file

@ -6,22 +6,33 @@ class App.DashboardActivityStream extends App.Controller
constructor: -> constructor: ->
super super
# @log 'aaaa', @el
@items = [] @items = []
# refresh list ever 140 sec.
@interval( @fetch, 140000, 'dashboard_activity_stream' )
fetch: =>
# use cache of first page
if window.LastRefresh[ 'dashboard_activity_stream' ]
@render( window.LastRefresh[ 'dashboard_activity_stream' ] )
# get data # get data
if @req
@req.abort()
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( @req = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/activity_stream', url: '/activity_stream',
data: { data: {
limit: @limit, limit: @limit,
} }
processData: true, processData: true,
# data: JSON.stringify( view: @view ), success: @load
success: (data, status, xhr) => )
@items = data.activity_stream
load: (data) =>
items = data.activity_stream
# load user collection # load user collection
@loadCollection( type: 'User', data: data.users ) @loadCollection( type: 'User', data: data.users )
@ -29,23 +40,24 @@ class App.DashboardActivityStream extends App.Controller
# load ticket collection # load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets ) @loadCollection( type: 'Ticket', data: data.tickets )
@render() # set cache
) window.LastRefresh[ 'dashboard_activity_stream' ] = items
@render(items)
render: -> render: (items) ->
# load user data # load user data
for item in @items for item in items
item.created_by = App.User.find(item.created_by_id) item.created_by = App.User.find(item.created_by_id)
# load ticket data # load ticket data
for item in @items for item in items
item.ticket = App.Ticket.find(item.o_id) item.ticket = App.Ticket.find(item.o_id)
html = App.view('dashboard/activity_stream')( html = App.view('dashboard/activity_stream')(
head: 'Activity Stream', head: 'Activity Stream',
items: @items items: items
) )
html = $(html) html = $(html)

View file

@ -11,8 +11,10 @@ class App.DashboardRecentViewed extends App.Controller
@items = [] @items = []
# get data # get data
if @req
@req.abort()
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( @req = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/recent_viewed', url: '/recent_viewed',
data: { data: {

View file

@ -3,13 +3,22 @@ $ = jQuery.sub()
class App.DashboardRss extends App.Controller class App.DashboardRss extends App.Controller
constructor: -> constructor: ->
super super
# @log 'aaaa', @el
@items = []
# refresh list ever 600 sec.
@interval( @fetch, 6000000, 'dashboard_rss' )
fetch: =>
# use cache of first page
if window.LastRefresh[ 'dashboard_rss' ]
@render( window.LastRefresh[ 'dashboard_rss' ] )
# get data # get data
if @req
@req.abort()
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( @req = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/rss_fetch', url: '/rss_fetch',
data: { data: {
@ -17,15 +26,21 @@ class App.DashboardRss extends App.Controller
url: @url, url: @url,
} }
processData: true, processData: true,
success: (data, status, xhr) => success: @load
@items = data.items || []
@render()
) )
render: -> load: (data) =>
items = data.items || []
# set cache
window.LastRefresh[ 'dashboard_rss' ] = items
@render(items)
render: (items) ->
html = App.view('dashboard/rss')( html = App.view('dashboard/rss')(
head: @head, head: @head,
items: @items items: items
) )
html = $(html) html = $(html)
@html html @html html

View file

@ -13,13 +13,23 @@ class App.DashboardTicket extends App.Controller
@start_page = 1 @start_page = 1
@navupdate '#' @navupdate '#'
@fetch() # refresh list ever 60 sec.
@interval( @fetch, 60000, 'dashboard_ticket_overview_' + @view )
fetch: -> fetch: =>
# set new key
@key = @view
# use cache of first page
if window.LastRefresh[ @key ] && @start_page is 1
@render( window.LastRefresh[ @key ] )
# get data # get data
if @req
@req.abort()
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( @req = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/ticket_overviews', url: '/ticket_overviews',
data: { data: {
@ -28,17 +38,18 @@ class App.DashboardTicket extends App.Controller
start_page: @start_page, start_page: @start_page,
} }
processData: true, processData: true,
# data: JSON.stringify( view: @view ), success: @load
success: (data, status, xhr) => )
load: (data) =>
# get meta data # get meta data
@overview = data.overview App.Overview.refresh( data.overview, options: { clear: true } )
App.Overview.refresh( @overview, options: { clear: true } )
App.Overview.unbind('local:rerender') App.Overview.unbind('local:rerender')
App.Overview.bind 'local:rerender', (record) => App.Overview.bind 'local:rerender', (record) =>
@log 'rerender...', record @log 'rerender...', record
@render() @render(data)
App.Overview.unbind('local:refetch') App.Overview.unbind('local:refetch')
App.Overview.bind 'local:refetch', (record) => App.Overview.bind 'local:refetch', (record) =>
@ -51,13 +62,16 @@ class App.DashboardTicket extends App.Controller
# load ticket collection # load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets ) @loadCollection( type: 'Ticket', data: data.tickets )
@tickets = data.tickets # set cache
window.LastRefresh[ @key ] = data
@render( data )
render: (data) ->
@overview = data.overview
@tickets_count = data.tickets_count @tickets_count = data.tickets_count
@tickets = data.tickets
@render()
)
render: ->
pages_total = parseInt( ( @tickets_count / @overview.view.d.per_page ) + 0.99999 ) || 1 pages_total = parseInt( ( @tickets_count / @overview.view.d.per_page ) + 0.99999 ) || 1
html = App.view('dashboard/ticket')( html = App.view('dashboard/ticket')(
@ -80,7 +94,7 @@ class App.DashboardTicket extends App.Controller
if _.isEmpty(@tickets) if _.isEmpty(@tickets)
table = '' table = ''
# table = '-none-' table = '-none-'
# append content table # append content table
html.find('.table-overview').append(table) html.find('.table-overview').append(table)

View file

@ -29,7 +29,9 @@ class Index extends App.Controller
fetch: () -> fetch: () ->
# get data # get data
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( if @req
@req.abort()
@req = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/ticket_create', url: '/ticket_create',
data: { data: {

View file

@ -31,10 +31,11 @@ class Index extends App.Controller
fetch: -> fetch: ->
# get data # get data
if @req
@req.abort()
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( @req = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/ticket_overviews', url: '/ticket_overviews',
data: { data: {

View file

@ -10,10 +10,10 @@ class App.Navigation extends App.Controller
@render() @render()
sync_ticket_overview = => sync_ticket_overview = =>
@sync(@ticket_overview, 30000,'interval_id_ticket_overview') @interval( @ticket_overview, 30000, 'nav_ticket_overview' )
sync_recent_viewed = => sync_recent_viewed = =>
@sync(@recent_viewed, 40000, 'interval_id_recent_viewed') @interval( @recent_viewed, 40000, 'nav_recent_viewed' )
Spine.bind 'navupdate', (data) => Spine.bind 'navupdate', (data) =>
@update(arguments[0]) @update(arguments[0])
@ -29,8 +29,6 @@ class App.Navigation extends App.Controller
# rerender if new overview data is there # rerender if new overview data is there
@delay( sync_ticket_overview, 800 ) @delay( sync_ticket_overview, 800 )
@delay( sync_ticket_overview, 2000 )
@delay( sync_recent_viewed, 1000 ) @delay( sync_recent_viewed, 1000 )
render: (user) -> render: (user) ->
@ -128,24 +126,6 @@ class App.Navigation extends App.Controller
@el.find("[href=\"#{url}\"]").parents('li').addClass('active') @el.find("[href=\"#{url}\"]").parents('li').addClass('active')
# @el.find("[href*=\"#{url}\"]").parents('li').addClass('active') # @el.find("[href*=\"#{url}\"]").parents('li').addClass('active')
sync: (action, interval, interval_id) =>
# check global var
if !@intervalID
@intervalID = {}
action()
# auto save
every = (ms, cb) -> setInterval cb, ms
# clear auto save
clearInterval(@intervalID[interval_id]) if @intervalID[interval_id]
# request new data
@intervalID[interval_id] = every interval, () =>
action()
# get data # get data
ticket_overview: => ticket_overview: =>
@ -158,8 +138,12 @@ class App.Navigation extends App.Controller
if !window.Session['id'] if !window.Session['id']
return return
# only of lod request is already done
if !@req_overview
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( # @req_overview.abort()
@req_overview = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/ticket_overviews', url: '/ticket_overviews',
data: {}, data: {},
@ -184,8 +168,10 @@ class App.Navigation extends App.Controller
# rebuild navbar # rebuild navbar
Spine.trigger 'navrebuild', window.Session Spine.trigger 'navrebuild', window.Session
)
# reset ajax call
@req_overview = undefined
)
# get data # get data
recent_viewed: => recent_viewed: =>
@ -199,8 +185,10 @@ class App.Navigation extends App.Controller
if !window.Session['id'] if !window.Session['id']
return return
# only of lod request is already done
if !@req_recent_viewed
@ajax = new App.Ajax @ajax = new App.Ajax
@ajax.ajax( @req_recent_viewed = @ajax.ajax(
type: 'GET', type: 'GET',
url: '/recent_viewed', url: '/recent_viewed',
data: { data: {
@ -246,4 +234,7 @@ class App.Navigation extends App.Controller
# rebuild navbar # rebuild navbar
Spine.trigger 'navrebuild', window.Session Spine.trigger 'navrebuild', window.Session
# reset ajax call
@req_recent_viewed = undefined
) )

View file

@ -6,3 +6,4 @@ Config.product_name = 'Zammad'
Config.requested_url = '' Config.requested_url = ''
var Store = {}; var Store = {};
var Session = {}; var Session = {};
var LastRefresh = {};