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
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',

View file

@ -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'

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.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 )

View file

@ -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

View file

@ -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 = {}

View file

@ -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

View file

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