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.
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.
Component’s persistent state.
# File lib/netzke/state.rb, line 48 def global_state @global_state ||= (global_state_manager.try(:state) || {}).symbolize_keys end
A string which will identify the component in persistence subsystem. If persistence_key is passed, use it. Otherwise use global_id.
# File lib/netzke/state.rb, line 25 def persistence_key initial_config[:persistence_key] ? initial_config[:persistence_key] : global_id end
Options merged into component’s configuration right after default and user-passed config, thus being reflected in +Netzke::Base#independent_config+ (see Netzke::Configuration).
# File lib/netzke/state.rb, line 59 def persistent_options (state[:persistent_options] || {}).symbolize_keys end
Component’s persistent state.
# File lib/netzke/state.rb, line 30 def state @state ||= (state_manager.try(:state) || {}).symbolize_keys end
Merges passed hash into component’s state.
# 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
Updates persistent_options
# 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
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)
# 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
Initialized state manager class, configured for managing global (not component specific) settings. At this moment this class has current_user and session set.
# File lib/netzke/state.rb, line 82 def global_state_manager Netzke::Core.persistence_manager_class && Netzke::Core.persistence_manager_class.init({ :current_user => Netzke::Core.controller.respond_to?(:current_user) && Netzke::Core.controller.current_user, :session => Netzke::Core.session }) end
Initialized state manager class. At this moment this class has current_user, component, and session set.
# File lib/netzke/state.rb, line 73 def state_manager Netzke::Core.persistence_manager_class && Netzke::Core.persistence_manager_class.init({ :component => persistence_key, :current_user => Netzke::Core.controller.respond_to?(:current_user) && Netzke::Core.controller.current_user, :session => Netzke::Core.session }) end
Generated with the Darkfish Rdoc Generator 2.