From a6f71a0bd79509686d4401a47beee1166123864a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 23 Oct 2012 22:25:42 +0200 Subject: [PATCH] Improved reload of full collections. --- app/assets/javascripts/app/lib/auth.js.coffee | 2 +- .../javascripts/app/lib/collection.js.coffee | 31 +++++++++++++++++++ app/controllers/sessions/collection_base.rb | 4 +-- .../sessions/collection_network.rb | 4 +-- app/controllers/sessions/collection_ticket.rb | 4 +-- app/controllers/sessions_controller.rb | 4 +-- lib/session_helper.rb | 8 ++--- lib/web_socket.rb | 6 ++-- 8 files changed, 47 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/app/lib/auth.js.coffee b/app/assets/javascripts/app/lib/auth.js.coffee index 6db8e4618..a18e1c8a1 100644 --- a/app/assets/javascripts/app/lib/auth.js.coffee +++ b/app/assets/javascripts/app/lib/auth.js.coffee @@ -66,7 +66,7 @@ class App.Auth # refresh/load default collections for key, value of data.default_collections - App.Collection.load( type: key, data: value ) + App.Collection.rest( type: key, data: value ) # rebuild navbar with new navbar items Spine.trigger 'navrebuild', data.session diff --git a/app/assets/javascripts/app/lib/collection.js.coffee b/app/assets/javascripts/app/lib/collection.js.coffee index 6164f5caa..e6a35e3a0 100644 --- a/app/assets/javascripts/app/lib/collection.js.coffee +++ b/app/assets/javascripts/app/lib/collection.js.coffee @@ -9,6 +9,11 @@ class App.Collection _instance ?= new _Singleton _instance.load( args ) + @reset: ( args ) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.reset( args ) + @find: ( type, id, callback, force ) -> if _instance == undefined _instance ?= new _Singleton @@ -58,6 +63,16 @@ class _Singleton console.log 'loadCollection:trigger', type, data.collections[type] @load( localStorage: data.localStorage, type: type, data: data.collections[type] ) + # add trigger - bind new events + Spine.bind 'restCollection', (data) => + + # load collections + if data.collections + for type of data.collections + + console.log 'restCollection:trigger', type, data.collections[type] + @reset( localStorage: data.localStorage, type: type, data: data.collections[type] ) + # find collections to load @_loadCollectionAll() @@ -71,6 +86,22 @@ class _Singleton console.log('load INIT', data) @load( data ) + reset: (params) -> + console.log( 'reset', params ) + + # remove permanent storage + list = App.Store.list() + for key in list + parts = key.split('::') + if parts[0] is 'collection' && parts[1] is params.type + App.Store.delete(key) + + # empty in-memory + App[ params.type ].refresh( [], { clear: true } ) + + # load with new data + @load(params) + load: (params) -> console.log( 'load', params ) diff --git a/app/controllers/sessions/collection_base.rb b/app/controllers/sessions/collection_base.rb index 8c114099c..0e1266089 100644 --- a/app/controllers/sessions/collection_base.rb +++ b/app/controllers/sessions/collection_base.rb @@ -1,5 +1,5 @@ module ExtraCollection - def session(collections) + def session( collections, user ) # all base stuff collections['Role'] = Role.all @@ -7,7 +7,7 @@ module ExtraCollection collections['Organization'] = Organization.all end - def push(collections) + def push( collections, user ) # all base stuff collections['Role'] = Role.all diff --git a/app/controllers/sessions/collection_network.rb b/app/controllers/sessions/collection_network.rb index f0a39ab58..31d92c552 100644 --- a/app/controllers/sessions/collection_network.rb +++ b/app/controllers/sessions/collection_network.rb @@ -1,5 +1,5 @@ module ExtraCollection - def session(collections) + def session( collections, user ) collections['Network'] = Network.all collections['NetworkCategory'] = Network::Category.all @@ -7,7 +7,7 @@ module ExtraCollection collections['NetworkPrivacy'] = Network::Privacy.all end - def push(collections) + def push( collections, user ) collections['Network'] = Network.all collections['NetworkCategory'] = Network::Category.all diff --git a/app/controllers/sessions/collection_ticket.rb b/app/controllers/sessions/collection_ticket.rb index 460b1f923..a084226b3 100644 --- a/app/controllers/sessions/collection_ticket.rb +++ b/app/controllers/sessions/collection_ticket.rb @@ -1,5 +1,5 @@ module ExtraCollection - def session(collections) + def session( collections, user ) # all ticket stuff collections['TicketStateType'] = Ticket::StateType.all @@ -15,7 +15,7 @@ module ExtraCollection collections['EmailAddress'] = EmailAddress.all end - def push(collections) + def push( collections, user ) # all ticket stuff collections['TicketStateType'] = Ticket::StateType.all diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 688a26eae..39b68330a 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -21,7 +21,7 @@ class SessionsController < ApplicationController user = User.find_fulldata(user.id) # auto population of default collections - default_collection = SessionHelper::default_collections() + default_collection = SessionHelper::default_collections(user) # set session user_id session[:user_id] = user['id'] @@ -82,7 +82,7 @@ class SessionsController < ApplicationController user = User.user_data_full( user_id ) # auto population of default collections - default_collection = SessionHelper::default_collections() + default_collection = SessionHelper::default_collections(user) # return current session render :json => { diff --git a/lib/session_helper.rb b/lib/session_helper.rb index 17810a9df..98b9b90ef 100644 --- a/lib/session_helper.rb +++ b/lib/session_helper.rb @@ -1,5 +1,5 @@ module SessionHelper - def self.default_collections + def self.default_collections(user) # auto population collections, store all here default_collection = {} @@ -9,12 +9,12 @@ module SessionHelper files = Dir.glob( "#{dir}/app/controllers/sessions/collection_*.rb" ) for file in files load file - ExtraCollection.session(default_collection) + ExtraCollection.session( default_collection, user ) end return default_collection end - def self.push_collections + def self.push_collections(user) # auto population collections, store all here push_collections = {} @@ -24,7 +24,7 @@ module SessionHelper files = Dir.glob( "#{dir}/app/controllers/sessions/collection_*.rb" ) for file in files load file - ExtraCollection.push(push_collections) + ExtraCollection.push( push_collections, user ) end return push_collections diff --git a/lib/web_socket.rb b/lib/web_socket.rb index 721438b9b..3c87307ee 100644 --- a/lib/web_socket.rb +++ b/lib/web_socket.rb @@ -338,7 +338,7 @@ class UserState collections = CacheIn.get( cache_key ) if !collections collections = {} - push_collection = SessionHelper::push_collections() + push_collection = SessionHelper::push_collections(user) push_collection.each { | key, value | collections[ key ] = true } @@ -351,7 +351,7 @@ class UserState cache_key = @cache_key + '_push_collections_' + key if CacheIn.expired(cache_key) if push_collection.empty? - push_collection = SessionHelper::push_collections() + push_collection = SessionHelper::push_collections(user) end push_collection_cache = CacheIn.get( cache_key, { :re_expire => true } ) self.log 'notice', "---user - fetch push_collection data " + cache_key @@ -576,7 +576,7 @@ class ClientState data['collections'] = {} data['collections'][key] = push_collections self.transaction({ - :event => 'loadCollection', + :event => 'restCollection', :data => data, })