diff --git a/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee b/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee index 1da842eef..669ad9d83 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee @@ -59,19 +59,8 @@ class App.ControllerGenericNew extends App.ControllerModal class App.ControllerGenericEdit extends App.ControllerModal constructor: (params) -> super - @log 'ControllerGenericEditWindow', params - - # fetch item on demand - if App[ @genericObject ].exists( params.id ) - @item = App[ @genericObject ].find( params.id ) - @render() - else - App[ @genericObject ].bind 'refresh', => - @log 'changed....' - @item = App[ @genericObject ].find( params.id ) - @render() - App[ @genericObject ].unbind 'refresh' - App[ @genericObject ].fetch( id: params.id ) + @item = App[ @genericObject ].find( params.id ) + @render() render: -> @@ -126,9 +115,6 @@ class App.ControllerGenericIndex extends App.Controller constructor: -> super - # set controller to active - Config['ActiveController'] = @pageData.navupdate - # set title @title @pageData.title @@ -136,7 +122,17 @@ class App.ControllerGenericIndex extends App.Controller @navupdate @pageData.navupdate # bind render after a change is done - App[ @genericObject ].bind 'refresh change', @render + App.Collection.observe( + level: 'page', + collections: [ + { + collection: @genericObject, + event: 'refresh change', + callback: @render, + }, + ], + ) + App[ @genericObject ].bind 'ajaxError', (rec, msg) => @log 'ajax notice', msg.status if msg.status is 401 @@ -145,24 +141,14 @@ class App.ControllerGenericIndex extends App.Controller # alert('relogin') @navigate 'login' - # execute fetch, if needed - if !App[ @genericObject ].count() || true -# if !App[ @genericObject ].count() + # execute fetch + @render() - # prerender without content - @render() - - # fetch all -# @log 'oooo', App[ @genericObject ].model -# App[ @genericObject ].deleteAll() - App[ @genericObject ].fetch() - else - @render() + # fetch all + App[ @genericObject ].fetch() render: => - return if Config['ActiveController'] isnt @pageData.navupdate - objects = App.Collection.all( type: @genericObject, sortBy: @defaultSortBy || 'name', diff --git a/app/assets/javascripts/app/controllers/network.js.coffee b/app/assets/javascripts/app/controllers/network.js.coffee index bc09a0f41..82151422b 100644 --- a/app/assets/javascripts/app/controllers/network.js.coffee +++ b/app/assets/javascripts/app/controllers/network.js.coffee @@ -6,7 +6,7 @@ $.fn.item = -> elementID or= $(@).parents('[data-id]').data('id') Note.find(elementID) -class Index extends App.Controller +class Index extends App.Controller events: 'click [data-type=network-new]': 'network_new' 'click [data-type=network-edit]': 'network_edit' diff --git a/app/assets/javascripts/app/controllers/notes.js.coffee b/app/assets/javascripts/app/controllers/notes.js.coffee new file mode 120000 index 000000000..40c05c912 --- /dev/null +++ b/app/assets/javascripts/app/controllers/notes.js.coffee @@ -0,0 +1 @@ +/Users/me/src/notes/app/assets/javascripts/app/controllers/notes.js.coffee \ No newline at end of file diff --git a/app/assets/javascripts/app/lib/collection.js.coffee b/app/assets/javascripts/app/lib/collection.js.coffee index 7586e4d3d..fd0cd1493 100644 --- a/app/assets/javascripts/app/lib/collection.js.coffee +++ b/app/assets/javascripts/app/lib/collection.js.coffee @@ -49,6 +49,16 @@ class App.Collection _instance ?= new _Singleton _instance.fetch( type ) + @observe: (args) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.observe(args) + + @observeCleanUpLevel: (level) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.observeCleanUpLevel(level) + class _Singleton constructor: (@args) -> @@ -64,13 +74,13 @@ class _Singleton @load( localStorage: data.localStorage, type: type, data: data.collections[type] ) # add trigger - bind new events - Spine.bind 'restCollection', (data) => + Spine.bind 'resetCollection', (data) => # load collections if data.collections for type of data.collections - console.log 'restCollection:trigger', type, data.collections[type] + console.log 'resetCollection:trigger', type, data.collections[type] @reset( localStorage: data.localStorage, type: type, data: data.collections[type] ) # find collections to load @@ -109,22 +119,22 @@ class _Singleton localStorage = params.localStorage + # load full array once if _.isArray( params.data ) - for object in params.data -# console.log( 'load ARRAY', object) - if !localStorage || localStorage && !App[ params.type ].exists( object['id'] ) - App[ params.type ].refresh( object, options: { clear: true } ) + console.log( 'load ARRAY', params.data) + App[ params.type ].refresh( params.data ) - # remember in store if not already requested from local storage - if !localStorage + # remember in store if not already requested from local storage + if !localStorage + for object in params.data App.Store.write( 'collection::' + params.type + '::' + object.id, { type: params.type, localStorage: true, data: [ object ] } ) return + # load data from object # if _.isObject( params.data ) for key, object of params.data # console.log( 'load OB', object) - if !localStorage || localStorage && !App[ params.type ].exists( object['id'] ) - App[ params.type ].refresh( object, options: { clear: true } ) + App[ params.type ].refresh( object ) # remember in store if not already requested from local storage if !localStorage @@ -145,11 +155,15 @@ class _Singleton else console.log( 'find not loaded!', type, id ) if callback - App[type].bind 'refresh', -> + + # execute callback if record got loaded + App[type].one 'refresh', -> console.log 'loaded..' + type + '..', id - App[type].unbind 'refresh' + data = App.Collection.find( type, id ) callback( data ) + + # fetch object console.log 'loading..' + type + '..', id App[type].fetch( id: id ) return true @@ -304,3 +318,27 @@ class _Singleton ) return collection + observeCleanUpLevel: (level) -> + return if !@observeCurrent + return if !@observeCurrent[level] + @_observeUnbind( @observeCurrent[level] ) + + observe: (data) -> + if !@observeCurrent + @observeCurrent = {} + + if @observeCurrent[ data.level ] + @_observeUnbind( @observeCurrent[ data.level ] ) + + @observeCurrent[ data.level ] = data.collections + for observe in data.collections + events = observe.event.split(' ') + for event in events + App[ observe.collection ].bind( event, observe.callback ) + + _observeUnbind: (observers) -> + for observe in observers + events = observe.event.split(' ') + for event in events + App[ observe.collection ].unbind( event, observe.callback ) + diff --git a/app/assets/javascripts/app/lib/i18n.js.coffee b/app/assets/javascripts/app/lib/i18n.js.coffee index 218d0c774..413c91b98 100644 --- a/app/assets/javascripts/app/lib/i18n.js.coffee +++ b/app/assets/javascripts/app/lib/i18n.js.coffee @@ -94,7 +94,7 @@ class _Singleton @map[ object[1] ] = object[2] # load in collection if needed - App.Translation.refresh( { id: object[0], source: object[1], target: object[2], locale: @locale }, options: { clear: true } ) + App.Translation.refresh( { id: object[0], source: object[1], target: object[2], locale: @locale } ) error: (xhr, statusText, error) => console.log 'error', error, statusText, xhr.statusCode diff --git a/app/assets/javascripts/app/lib/interface_handle.js.coffee b/app/assets/javascripts/app/lib/interface_handle.js.coffee index 2df264d96..948a947f1 100644 --- a/app/assets/javascripts/app/lib/interface_handle.js.coffee +++ b/app/assets/javascripts/app/lib/interface_handle.js.coffee @@ -40,8 +40,8 @@ class App.Content extends Spine.Controller do (route, callback) => @route(route, (params) -> - # remember current controller - Config['ActiveController'] = route + # remove observers for page + App.Collection.observeCleanUpLevel('page') # send current controller params_only = {} diff --git a/app/controllers/sessions/collection_ticket.rb b/app/controllers/sessions/collection_ticket.rb index a084226b3..a867c9813 100644 --- a/app/controllers/sessions/collection_ticket.rb +++ b/app/controllers/sessions/collection_ticket.rb @@ -30,6 +30,12 @@ module ExtraCollection # all email addresses collections['EmailAddress'] = EmailAddress.all + # all templates + collections['Template'] = Template.all + + # all text modules + collections['TextModule'] = TextModule.all + end module_function :session, :push diff --git a/lib/web_socket.rb b/lib/web_socket.rb index 3c87307ee..fec035043 100644 --- a/lib/web_socket.rb +++ b/lib/web_socket.rb @@ -576,7 +576,7 @@ class ClientState data['collections'] = {} data['collections'][key] = push_collections self.transaction({ - :event => 'restCollection', + :event => 'resetCollection', :data => data, })