Split of model and controller.

This commit is contained in:
Martin Edenhofer 2015-11-04 14:24:45 +01:00
parent 7b9e7ea0bc
commit c2459ed72d
2 changed files with 84 additions and 52 deletions

View file

@ -1,6 +1,6 @@
class App.TicketOverview extends App.Controller class App.TicketOverview extends App.Controller
className: 'overviews' className: 'overviews'
constructor: -> constructor: ->
super super
@ -10,16 +10,16 @@ class App.TicketOverview extends App.Controller
@html App.view('ticket_overview')() @html App.view('ticket_overview')()
@navBarControllerVertical = new Navbar @navBarControllerVertical = new Navbar
el: @el.find('.overview-header') el: @$('.overview-header')
view: @view view: @view
vertical: true vertical: true
@navBarController = new Navbar @navBarController = new Navbar
el: @el.find('.sidebar') el: @$('.sidebar')
view: @view view: @view
@contentController = new Table @contentController = new Table
el: @el.find('.overview-table') el: @$('.overview-table')
view: @view view: @view
active: (state) => active: (state) =>
@ -686,34 +686,23 @@ class Navbar extends App.Controller
constructor: -> constructor: ->
super super
console.log('RR', @vertical)
# rebuild ticket overview data App.OverviewIndexCollection.bind(@render)
@bind 'ticket_overview_index', (data) =>
#console.log('EVENT ticket_overview_index')
@cache = data
@update()
App.OverviewIndexCollection.fetch()
# force fetch ticket overview # force fetch ticket overview
@bind 'ticket_overview_fetch_force', => #@bind 'ticket_overview_fetch_force', =>
@fetch() # @fetch()
# rerender view, e. g. on langauge change # rerender view, e. g. on langauge change
@bind 'ui:rerender', => @bind 'ui:rerender', =>
@render() @render(App.OverviewIndexCollection.get())
if @options.vertical if @options.vertical
$(window).on 'resize.navbar', @autoFoldTabs $(window).on 'resize.navbar', @autoFoldTabs
# init fetch via ajax
ajaxInit = =>
# ignore if already pushed via websockets
return if @cache
@fetch()
@delay( ajaxInit, 5000 )
navigate: (event) => navigate: (event) =>
location.hash = $(event.currentTarget).attr('data-target') location.hash = $(event.currentTarget).attr('data-target')
@ -731,8 +720,9 @@ class Navbar extends App.Controller
$(window).off 'resize.navbar', @autoFoldTabs $(window).off 'resize.navbar', @autoFoldTabs
autoFoldTabs: => autoFoldTabs: =>
items = App.OverviewIndexCollection.get()
@html App.view("agent_ticket_view/navbar#{ if @options.vertical then '_vertical' }") @html App.view("agent_ticket_view/navbar#{ if @options.vertical then '_vertical' }")
items: @data items: items
while @clone.width() > @el.width() while @clone.width() > @el.width()
@tabClone.not('.hide').last().addClass('hide') @tabClone.not('.hide').last().addClass('hide')
@ -745,49 +735,32 @@ class Navbar extends App.Controller
@dropdown.remove() @dropdown.remove()
@dropdownToggle.remove() @dropdownToggle.remove()
fetch: =>
#console.log('AJAX CALLL')
# init fetch via ajax, all other updates on time via websockets
@ajax(
id: 'ticket_overviews',
type: 'GET',
url: @apiPath + '/ticket_overviews',
processData: true,
success: (data) =>
@cache = data
@update()
)
active: (state) => active: (state) =>
@activeState = state @activeState = state
update: (params = {}) -> update: (params = {}) ->
for key, value of params for key, value of params
@[key] = value @[key] = value
@render() @render(App.OverviewIndexCollection.get())
if @activeState render: (data) =>
meta = return if !data
title: ''
if @cache
for item in @cache
if item.link is @view
meta.title = item.name
@title meta.title, true
render: => # set page title
return if !@cache if @activeState && @view
@data = _.clone(@cache) for item in data
if item.link is @view
@title item.name, true
# redirect to first view # redirect to first view
if @activeState && !@view && !_.isEmpty(@data) if @activeState && !@view
view = @data[0].link view = data[0].link
#console.log('REDIRECT', "ticket/view/#{view}") #console.log('REDIRECT', "ticket/view/#{view}")
@navigate "ticket/view/#{view}", true @navigate "ticket/view/#{view}", true
return return
# add new views # add new views
for item in @data for item in data
item.target = '#ticket/view/' + item.link item.target = '#ticket/view/' + item.link
if item.link is @view if item.link is @view
item.active = true item.active = true
@ -796,7 +769,7 @@ class Navbar extends App.Controller
item.active = false item.active = false
@html App.view("agent_ticket_view/navbar#{ if @options.vertical then '_vertical' else '' }") @html App.view("agent_ticket_view/navbar#{ if @options.vertical then '_vertical' else '' }")
items: @data items: data
if @options.vertical if @options.vertical
@autoFoldTabs() @autoFoldTabs()

View file

@ -0,0 +1,59 @@
class App.OverviewIndexCollection
_instance = undefined # Must be declared here to force the closure on the class
@get: ->
if _instance == undefined
_instance ?= new _Singleton
_instance.get()
@bind: (callback) ->
if _instance == undefined
_instance ?= new _Singleton
_instance.bind(callback)
@unbind: (callback) ->
if _instance == undefined
_instance ?= new _Singleton
_instance.unbind(callback)
@fetch: ->
if _instance == undefined
_instance ?= new _Singleton
_instance.fetch()
# The actual Singleton class
class _Singleton
constructor: ->
@callbacks = {}
@counter = 0
App.Event.bind 'ticket_overview_index', (data) =>
@overview_index = data
get: ->
@overview_index
bind: (callback) ->
@counter += 1
@callbacks[@counter] = callback
unbind: (callback) ->
for counter, localCallback of @callbacks
if callback is localCallback
delete @callbacks[counter]
fetch: =>
return if @fetchActive
@fetchActive = true
App.Ajax.request(
id: 'ticket_overviews',
type: 'GET',
url: App.Config.get('api_path') + '/ticket_overviews',
processData: true,
success: (data) =>
@fetchActive = false
@overview_index = data
for counter, callback of @callbacks
callback(data)
error: =>
@fetchActive = false
)