Local storage allows to get or set value of named key, with namespace prefix, so that it will not conflict with other components.
Initializing local storage
Example of initialization
This should be placed in widget's
init method, but can also be in other places,
just ensure that this code is executed when creating widget.
The first argument is owner widget, which will also be used to reserve namespace
for this widget keys. The second
$namespace parameter can be used to create
additional, local namespace for keys and in most cases can be left empty.
... $storage = new LocalStorage($this); $params = [ $storage ]; $this->jsRef = new JsWidget($this, $params); ...
Setting value in PHP
It is possible to set local storage value from PHP code when initializing widget.
class, which in turn will set namespaced key to passed value. The value will
Getting value in PHP
While it is not really possible to obtain local storage value in PHP, the
myMethod, with argument
get method call.
$ref = $storage->get('myKey'); $this->jsRef->myMethod($ref);
The usage of local storage class is more obvious and straightforward than the PHP counterpart. It
set methods. The class should already be initialized
by PHP part.
The example below is in
class MyWidget # @var Maslosoft.Widgets.LocalStorage storage: null constructor: (@storage) -> # Get storage value val = @storage.get 'active' toggle: () => isActive = @storage.get 'active' @storage.set 'active', !isActive