Module: Netzke::State

Included in:
Base
Defined in:
lib/netzke/state.rb

Overview

When a persistence subsystem (such as netzke-persistence) is used, a widget can store its state using the update_state method that accepts a hash, e.g.:

update_state(:position => {:x => 100, :y => 200})

Later the state can be retrieved by calling the state method:

state[:position] #=> {:x => 100, :y => 200}

To enable persistence for a specific component, configure it with persistence option set to true.

Sharing state

Different components can share the state by sharing the persistence key, which can be provided as configuration option, e.g.:

netzke :books, :class_name => "Basepack::GridPanel", :persistence_key => "books_state_identifier"
netzke :deleted_books, :class_name => "Basepack::GridPanel", :persistence_key => "books_state_identifier"

Make sure that the provided persistence_key has effect on application level, not only within the view. By default persistence_key is set to component's global id. Thus, two components named equally will share the state even being used in different views.

Instance Method Summary (collapse)

Instance Method Details

- (Object) global_state

Component's persistent state.



48
49
50
# File 'lib/netzke/state.rb', line 48

def global_state
  @global_state ||= (global_state_manager.try(:state) || {}).symbolize_keys
end

- (Object) persistence_key

A string which will identify the component in persistence subsystem. If persistence_key is passed, use it. Otherwise use global_id.



25
26
27
# File 'lib/netzke/state.rb', line 25

def persistence_key
  initial_config[:persistence_key] ? initial_config[:persistence_key] : global_id
end

- (Object) persistent_options

Options merged into component's configuration right after default and user-passed config, thus being reflected in Netzke::Base#independent_config (see Netzke::Configuration).



59
60
61
# File 'lib/netzke/state.rb', line 59

def persistent_options
  (state[:persistent_options] || {}).symbolize_keys
end

- (Object) state

Component's persistent state.



30
31
32
# File 'lib/netzke/state.rb', line 30

def state
  @state ||= (state_manager.try(:state) || {}).symbolize_keys
end

- (Object) update_global_state(hsh)

Merges passed hash into component's state.



53
54
55
56
# File 'lib/netzke/state.rb', line 53

def update_global_state(hsh)
  global_state_manager.try(:update_state!, hsh)
  @global_state = nil # reset cache
end

- (Object) update_persistent_options(hsh)

Updates persistent_options



64
65
66
67
68
# File 'lib/netzke/state.rb', line 64

def update_persistent_options(hsh)
  new_persistent_options = persistent_options.merge(hsh)
  new_persistent_options.delete_if{ |k,v| v.nil? } # setting values to nil means deleting them
  update_state(:persistent_options => new_persistent_options)
end

- (Object) update_state(*args)

Merges passed hash into component's state. Can also accept 2 arguments which will be treated as a hash pair. E.g.:

update_state(:peoples_most_feared_number, 13)

is equivalent to:

update_state(:peoples_most_feared_number => 13)


42
43
44
45
# File 'lib/netzke/state.rb', line 42

def update_state(*args)
  state_manager.try(:update_state!, args.first.is_a?(Hash) ? args.first : {args.first => args.last})
  @state = nil # reset cache
end