Module: Netzke::Core::State

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

Overview

A component can access and update its state through the state method, e.g.:

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

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

Default implementation uses session to store stateful data, but can be implemented differently by 3rd-party gems.

Sharing state

Different components can share the state by specifying the same persistent_key option.

Note that the provided persistence_key has effect on application level, not only within the view. By default persistence_key is set to component’s js_id. Thus, _two components named equally will share the state even being used in different Rails views_.

Defined Under Namespace

Classes: StateProxy

Instance Method Summary collapse

Instance Method Details

#persistence_keyObject

A string which identifies the component. Can be passed as persistence_key config option. Two components with the same persistence_key will be sharing the state. If persistence_key is passed in the config, use it. Otherwise use js_id.



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

def persistence_key
  (config.persistence_key || js_id).to_sym
end

#stateObject

Component’s persistent state.

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

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

May be overridden by persistence subsystems. The object returned by this should implement the following methods:

  • []=

  • delete(key)

  • clear



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

def state
  @state_proxy ||= StateProxy.new(persistence_key)
end