Session storage

Metro 4 provides special object to work with browser session storage engine.

About

Object fro work with session storage defined in Metro.session and contains next methods:

  • setKey(key) - define main storage key (default: "METRO:APP")
  • setKey() - get main storage key
  • setItem(key, value) - store any data to key
  • getItem(key, default) - get stored values with key.
  • getItemPart(key, sub_key, default) - get part of stored values with key.
  • delItem(key) - delete data with key.

Init storage

To init storage execute method setKey. This method sets key for your app data in storage. This is a prefix for all your stored data. Method getKey return main key.


                    Metro.session.setKey('MY_APP');
                    Metro.session.getKey();
                

Store data

To store data in storage execute method setItem.


                    var key = 'user',
                        data = {
                            'Bill': {
                                'FirstName': 'Billy',
                                'LastName': 'Gates',
                                'Company': 'Microsoft'
                            },
                            'Steve': {
                                'FirstName': 'Steve',
                                'LastName': 'Jobs',
                                'Company': 'Apple'
                            }
                        };

                    Metro.session.setItem(key, data);
                

Get data

To get data you can use methods: getItem or getItemPart. Method getItem return data stored with key. Method getItemPart return part of data stored with key.

getItem

This method have next parameters: key - key for stored data, default - value returned if key not exist in storage.


                    var key = 'user',
                        default_value = {};

                    Metro.session.getItem(key, default_value);
                

getItemPart

This method have next parameters: key - key for stored data, sub_key - subkey for stored data, default - value returned if key not exist in storage.


                    var key = 'user',
                        subkey = 'Bill',
                        subkey2 = 'Bill->LastName',
                        default_value = {};

                    Metro.session.getItemPart(key, subkey, default_value);
                    Metro.session.getItemPart(key, subkey2, default_value);
                

Delete data

To delete data you can use method delItem.


                    var key = 'user';

                    Metro.session.delItem(key);