Improved observing ob whole collections.

This commit is contained in:
Martin Edenhofer 2012-10-24 20:20:56 +02:00
parent c28406b0ac
commit d83b08d7d0
8 changed files with 79 additions and 48 deletions

View file

@ -59,19 +59,8 @@ class App.ControllerGenericNew extends App.ControllerModal
class App.ControllerGenericEdit extends App.ControllerModal class App.ControllerGenericEdit extends App.ControllerModal
constructor: (params) -> constructor: (params) ->
super super
@log 'ControllerGenericEditWindow', params
# fetch item on demand
if App[ @genericObject ].exists( params.id )
@item = App[ @genericObject ].find( params.id ) @item = App[ @genericObject ].find( params.id )
@render() @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 )
render: -> render: ->
@ -126,9 +115,6 @@ class App.ControllerGenericIndex extends App.Controller
constructor: -> constructor: ->
super super
# set controller to active
Config['ActiveController'] = @pageData.navupdate
# set title # set title
@title @pageData.title @title @pageData.title
@ -136,7 +122,17 @@ class App.ControllerGenericIndex extends App.Controller
@navupdate @pageData.navupdate @navupdate @pageData.navupdate
# bind render after a change is done # 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) => App[ @genericObject ].bind 'ajaxError', (rec, msg) =>
@log 'ajax notice', msg.status @log 'ajax notice', msg.status
if msg.status is 401 if msg.status is 401
@ -145,24 +141,14 @@ class App.ControllerGenericIndex extends App.Controller
# alert('relogin') # alert('relogin')
@navigate 'login' @navigate 'login'
# execute fetch, if needed # execute fetch
if !App[ @genericObject ].count() || true
# if !App[ @genericObject ].count()
# prerender without content
@render() @render()
# fetch all # fetch all
# @log 'oooo', App[ @genericObject ].model
# App[ @genericObject ].deleteAll()
App[ @genericObject ].fetch() App[ @genericObject ].fetch()
else
@render()
render: => render: =>
return if Config['ActiveController'] isnt @pageData.navupdate
objects = App.Collection.all( objects = App.Collection.all(
type: @genericObject, type: @genericObject,
sortBy: @defaultSortBy || 'name', sortBy: @defaultSortBy || 'name',

View file

@ -0,0 +1 @@
/Users/me/src/notes/app/assets/javascripts/app/controllers/notes.js.coffee

View file

@ -49,6 +49,16 @@ class App.Collection
_instance ?= new _Singleton _instance ?= new _Singleton
_instance.fetch( type ) _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 class _Singleton
constructor: (@args) -> constructor: (@args) ->
@ -64,13 +74,13 @@ class _Singleton
@load( localStorage: data.localStorage, type: type, data: data.collections[type] ) @load( localStorage: data.localStorage, type: type, data: data.collections[type] )
# add trigger - bind new events # add trigger - bind new events
Spine.bind 'restCollection', (data) => Spine.bind 'resetCollection', (data) =>
# load collections # load collections
if data.collections if data.collections
for type of 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] ) @reset( localStorage: data.localStorage, type: type, data: data.collections[type] )
# find collections to load # find collections to load
@ -109,22 +119,22 @@ class _Singleton
localStorage = params.localStorage localStorage = params.localStorage
# load full array once
if _.isArray( params.data ) if _.isArray( params.data )
for object in params.data console.log( 'load ARRAY', params.data)
# console.log( 'load ARRAY', object) App[ params.type ].refresh( params.data )
if !localStorage || localStorage && !App[ params.type ].exists( object['id'] )
App[ params.type ].refresh( object, options: { clear: true } )
# remember in store if not already requested from local storage # remember in store if not already requested from local storage
if !localStorage if !localStorage
for object in params.data
App.Store.write( 'collection::' + params.type + '::' + object.id, { type: params.type, localStorage: true, data: [ object ] } ) App.Store.write( 'collection::' + params.type + '::' + object.id, { type: params.type, localStorage: true, data: [ object ] } )
return return
# load data from object
# if _.isObject( params.data ) # if _.isObject( params.data )
for key, object of params.data for key, object of params.data
# console.log( 'load OB', object) # console.log( 'load OB', object)
if !localStorage || localStorage && !App[ params.type ].exists( object['id'] ) App[ params.type ].refresh( object )
App[ params.type ].refresh( object, options: { clear: true } )
# remember in store if not already requested from local storage # remember in store if not already requested from local storage
if !localStorage if !localStorage
@ -145,11 +155,15 @@ class _Singleton
else else
console.log( 'find not loaded!', type, id ) console.log( 'find not loaded!', type, id )
if callback if callback
App[type].bind 'refresh', ->
# execute callback if record got loaded
App[type].one 'refresh', ->
console.log 'loaded..' + type + '..', id console.log 'loaded..' + type + '..', id
App[type].unbind 'refresh'
data = App.Collection.find( type, id ) data = App.Collection.find( type, id )
callback( data ) callback( data )
# fetch object
console.log 'loading..' + type + '..', id console.log 'loading..' + type + '..', id
App[type].fetch( id: id ) App[type].fetch( id: id )
return true return true
@ -304,3 +318,27 @@ class _Singleton
) )
return collection 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 )

View file

@ -94,7 +94,7 @@ class _Singleton
@map[ object[1] ] = object[2] @map[ object[1] ] = object[2]
# load in collection if needed # 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) => error: (xhr, statusText, error) =>
console.log 'error', error, statusText, xhr.statusCode console.log 'error', error, statusText, xhr.statusCode

View file

@ -40,8 +40,8 @@ class App.Content extends Spine.Controller
do (route, callback) => do (route, callback) =>
@route(route, (params) -> @route(route, (params) ->
# remember current controller # remove observers for page
Config['ActiveController'] = route App.Collection.observeCleanUpLevel('page')
# send current controller # send current controller
params_only = {} params_only = {}

View file

@ -30,6 +30,12 @@ module ExtraCollection
# all email addresses # all email addresses
collections['EmailAddress'] = EmailAddress.all collections['EmailAddress'] = EmailAddress.all
# all templates
collections['Template'] = Template.all
# all text modules
collections['TextModule'] = TextModule.all
end end
module_function :session, :push module_function :session, :push

View file

@ -576,7 +576,7 @@ class ClientState
data['collections'] = {} data['collections'] = {}
data['collections'][key] = push_collections data['collections'][key] = push_collections
self.transaction({ self.transaction({
:event => 'restCollection', :event => 'resetCollection',
:data => data, :data => data,
}) })