From 419ed6571c3f379187e7783fd9860c10314551c8 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 15 Dec 2012 00:02:58 +0100 Subject: [PATCH] improved push of data with has changed and was already pushed. --- .../_application_controller.js.coffee | 7 ++- .../app/lib/app_post/collection.js.coffee | 8 +-- lib/session.rb | 54 +++++++++++++++++-- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index ff891b1a5..82642298e 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -140,10 +140,13 @@ class App.Controller extends Spine.Controller return string userInfo: (data) => + el = data.el || $('#customer_info') + el.unbind() + # start customer info controller new App.UserInfo( - el: data.el || $('#customer_info'), - user_id: data.user_id, + el: el + user_id: data.user_id ) authenticate: -> diff --git a/app/assets/javascripts/app/lib/app_post/collection.js.coffee b/app/assets/javascripts/app/lib/app_post/collection.js.coffee index a509912e2..0ba483885 100644 --- a/app/assets/javascripts/app/lib/app_post/collection.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/collection.js.coffee @@ -157,10 +157,8 @@ class _Singleton extends Spine.Module data = @_fillUp( type, data ) if callback callback( data ) - console.log 'find', type, data return data else - console.log 'find not exists', type, data if force @log 'Collection', 'debug', 'find forced to load!', type, id else @@ -173,7 +171,11 @@ class _Singleton extends Spine.Module data = App.Collection.find( type, id ) # load update to local storage - col.load( localStorage: false, type: type, data: [ data ], refresh: true ) + clone = {} + for key, value of data + if typeof value isnt 'function' + clone[key] = value + col.load( localStorage: false, type: type, data: [ clone ], refresh: true ) callback( data ) diff --git a/lib/session.rb b/lib/session.rb index 7636a4026..e12ceed17 100644 --- a/lib/session.rb +++ b/lib/session.rb @@ -446,6 +446,52 @@ class ClientState # remember last run CacheIn.set( 'last_run_' + user.id.to_s , true, { :expires_in => 20.seconds } ) + # verify already pushed data + if @pushed[:users] + users = {} + @pushed[:users].each {|user_id, user_o| + self.user( user_id, users ) + } + if !users.empty? + users.each {|user_id, user_data| + self.log 'notify', "push update of already pushed user id #{user_id}" + } + # send update to browser + self.send({ + :data => { + :collections => { + :User => users, + }, + }, + :event => [ 'loadCollection', 'ticket_overview_rebuild' ], + }); + end + end + + # verify already pushed data + if @pushed[:tickets] + tickets = [] + users = {} + @pushed[:tickets].each {|ticket_id, ticket_data| + self.ticket( ticket_id, tickets, users ) + } + if !tickets.empty? + tickets.each {|ticket_id| + self.log 'notify', "push update of already pushed ticket id #{ticket_id}" + } + # send update to browser + self.send({ + :data => { + :collections => { + :Ticket => tickets, + :User => users, + }, + }, + :event => [ 'loadCollection', 'ticket_overview_rebuild' ], + }); + end + end + # overview cache_key = @cache_key + '_overview' overview_time = CacheIn.get_time( cache_key, { :ignore_expire => true } ) @@ -640,8 +686,8 @@ class ClientState @pushed[:tickets] = {} end ticket = Ticket.full_data(ticket_id) - if @pushed[:tickets][ticket_id] != ticket - @pushed[:tickets][ticket_id] = ticket + if @pushed[:tickets][ticket_id] != ticket['updated_at'] + @pushed[:tickets][ticket_id] = ticket['updated_at'] tickets.push ticket end @@ -661,8 +707,8 @@ class ClientState user = User.user_data_full( user_id ) # user is already on client and not changed - return if @pushed[:users][ user_id ] == user - @pushed[:users][user_id] = user + return if @pushed[:users][ user_id ] == user['updated_at'] + @pushed[:users][user_id] = user['updated_at'] # user not on client or different self.log 'notice', 'push user ... ' + user['login']