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
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') ->
# remove old popovers
$('.popover-inner').parent().remove()
# show user popup
$('.user-data').popover(
delay: { show: 500, hide: 1200 },
@ -570,6 +592,10 @@ class App.Controller extends Spine.Controller
)
userTicketPopups: (data) ->
# remove old popovers
$('.popover-inner').parent().remove()
# get data
@tickets = {}
ajax = new App.Ajax

View file

@ -6,46 +6,58 @@ class App.DashboardActivityStream extends App.Controller
constructor: ->
super
# @log 'aaaa', @el
@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
if @req
@req.abort()
@ajax = new App.Ajax
@ajax.ajax(
@req = @ajax.ajax(
type: 'GET',
url: '/activity_stream',
data: {
limit: @limit,
}
processData: true,
# data: JSON.stringify( view: @view ),
success: (data, status, xhr) =>
@items = data.activity_stream
# load user collection
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
@render()
success: @load
)
render: ->
load: (data) =>
items = data.activity_stream
# load user collection
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
# set cache
window.LastRefresh[ 'dashboard_activity_stream' ] = items
@render(items)
render: (items) ->
# load user data
for item in @items
for item in items
item.created_by = App.User.find(item.created_by_id)
# load ticket data
for item in @items
for item in items
item.ticket = App.Ticket.find(item.o_id)
html = App.view('dashboard/activity_stream')(
head: 'Activity Stream',
items: @items
items: items
)
html = $(html)

View file

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

View file

@ -3,13 +3,22 @@ $ = jQuery.sub()
class App.DashboardRss extends App.Controller
constructor: ->
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
if @req
@req.abort()
@ajax = new App.Ajax
@ajax.ajax(
@req = @ajax.ajax(
type: 'GET',
url: '/rss_fetch',
data: {
@ -17,15 +26,21 @@ class App.DashboardRss extends App.Controller
url: @url,
}
processData: true,
success: (data, status, xhr) =>
@items = data.items || []
@render()
success: @load
)
render: ->
load: (data) =>
items = data.items || []
# set cache
window.LastRefresh[ 'dashboard_rss' ] = items
@render(items)
render: (items) ->
html = App.view('dashboard/rss')(
head: @head,
items: @items
items: items
)
html = $(html)
@html html

View file

@ -13,13 +13,23 @@ class App.DashboardTicket extends App.Controller
@start_page = 1
@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
if @req
@req.abort()
@ajax = new App.Ajax
@ajax.ajax(
@req = @ajax.ajax(
type: 'GET',
url: '/ticket_overviews',
data: {
@ -28,36 +38,40 @@ class App.DashboardTicket extends App.Controller
start_page: @start_page,
}
processData: true,
# data: JSON.stringify( view: @view ),
success: (data, status, xhr) =>
# get meta data
@overview = data.overview
App.Overview.refresh( @overview, options: { clear: true } )
App.Overview.unbind('local:rerender')
App.Overview.bind 'local:rerender', (record) =>
@log 'rerender...', record
@render()
App.Overview.unbind('local:refetch')
App.Overview.bind 'local:refetch', (record) =>
@log 'refetch...', record
@fetch()
# load user collection
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
@tickets = data.tickets
@tickets_count = data.tickets_count
@render()
success: @load
)
render: ->
load: (data) =>
# get meta data
App.Overview.refresh( data.overview, options: { clear: true } )
App.Overview.unbind('local:rerender')
App.Overview.bind 'local:rerender', (record) =>
@log 'rerender...', record
@render(data)
App.Overview.unbind('local:refetch')
App.Overview.bind 'local:refetch', (record) =>
@log 'refetch...', record
@fetch()
# load user collection
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
# set cache
window.LastRefresh[ @key ] = data
@render( data )
render: (data) ->
@overview = data.overview
@tickets_count = data.tickets_count
@tickets = data.tickets
pages_total = parseInt( ( @tickets_count / @overview.view.d.per_page ) + 0.99999 ) || 1
html = App.view('dashboard/ticket')(
@ -80,7 +94,7 @@ class App.DashboardTicket extends App.Controller
if _.isEmpty(@tickets)
table = ''
# table = '-none-'
table = '-none-'
# append content table
html.find('.table-overview').append(table)

View file

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

View file

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

View file

@ -10,10 +10,10 @@ class App.Navigation extends App.Controller
@render()
sync_ticket_overview = =>
@sync(@ticket_overview, 30000,'interval_id_ticket_overview')
@interval( @ticket_overview, 30000, 'nav_ticket_overview' )
sync_recent_viewed = =>
@sync(@recent_viewed, 40000, 'interval_id_recent_viewed')
@interval( @recent_viewed, 40000, 'nav_recent_viewed' )
Spine.bind 'navupdate', (data) =>
@update(arguments[0])
@ -29,8 +29,6 @@ class App.Navigation extends App.Controller
# rerender if new overview data is there
@delay( sync_ticket_overview, 800 )
@delay( sync_ticket_overview, 2000 )
@delay( sync_recent_viewed, 1000 )
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')
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
ticket_overview: =>
@ -158,34 +138,40 @@ class App.Navigation extends App.Controller
if !window.Session['id']
return
@ajax = new App.Ajax
@ajax.ajax(
type: 'GET',
url: '/ticket_overviews',
data: {},
processData: true,
success: (data, status, xhr) =>
# only of lod request is already done
# remove old views
for key of Config.NavBar
if Config.NavBar[key].parent is '#ticket/view'
delete Config.NavBar[key]
# add new views
for item in data
Config.NavBar['TicketOverview' + item.url] = {
prio: item.prio,
parent: '#ticket/view',
name: item.name,
count: item.count,
target: '#ticket/view/' + item.url,
role: ['Agent'],
}
# rebuild navbar
Spine.trigger 'navrebuild', window.Session
)
if !@req_overview
@ajax = new App.Ajax
# @req_overview.abort()
@req_overview = @ajax.ajax(
type: 'GET',
url: '/ticket_overviews',
data: {},
processData: true,
success: (data, status, xhr) =>
# remove old views
for key of Config.NavBar
if Config.NavBar[key].parent is '#ticket/view'
delete Config.NavBar[key]
# add new views
for item in data
Config.NavBar['TicketOverview' + item.url] = {
prio: item.prio,
parent: '#ticket/view',
name: item.name,
count: item.count,
target: '#ticket/view/' + item.url,
role: ['Agent'],
}
# rebuild navbar
Spine.trigger 'navrebuild', window.Session
# reset ajax call
@req_overview = undefined
)
# get data
recent_viewed: =>
@ -199,51 +185,56 @@ class App.Navigation extends App.Controller
if !window.Session['id']
return
@ajax = new App.Ajax
@ajax.ajax(
type: 'GET',
url: '/recent_viewed',
data: {
limit: 5,
}
processData: true,
success: (data, status, xhr) =>
# only of lod request is already done
if !@req_recent_viewed
@ajax = new App.Ajax
@req_recent_viewed = @ajax.ajax(
type: 'GET',
url: '/recent_viewed',
data: {
limit: 5,
}
processData: true,
success: (data, status, xhr) =>
items = data.recent_viewed
# load user collection
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
# remove old views
for key of Config.NavBarRight
if Config.NavBarRight[key].parent is '#current_user'
part = Config.NavBarRight[key].target.split '::'
if part is 'RecendViewed'
delete Config.NavBarRight[key]
# add new views
prio = 5000
for item in items
divider = false
navheader = false
if prio is 5000
divider = true
navheader = 'Recent Viewed'
ticket = App.Ticket.find(item.o_id)
prio++
Config.NavBarRight['RecendViewed::' + ticket.id] = {
prio: prio,
parent: '#current_user',
name: item.history_object.name + ' (' + ticket.title + ')',
target: '#ticket/zoom/' + ticket.id,
role: ['Agent'],
divider: divider,
navheader: navheader
}
# rebuild navbar
Spine.trigger 'navrebuild', window.Session
items = data.recent_viewed
# load user collection
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
# remove old views
for key of Config.NavBarRight
if Config.NavBarRight[key].parent is '#current_user'
part = Config.NavBarRight[key].target.split '::'
if part is 'RecendViewed'
delete Config.NavBarRight[key]
# add new views
prio = 5000
for item in items
divider = false
navheader = false
if prio is 5000
divider = true
navheader = 'Recent Viewed'
ticket = App.Ticket.find(item.o_id)
prio++
Config.NavBarRight['RecendViewed::' + ticket.id] = {
prio: prio,
parent: '#current_user',
name: item.history_object.name + ' (' + ticket.title + ')',
target: '#ticket/zoom/' + ticket.id,
role: ['Agent'],
divider: divider,
navheader: navheader
}
# rebuild navbar
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 = ''
var Store = {};
var Session = {};
var LastRefresh = {};