YUI recommends YUI3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This documentation is no longer maintained.
The Storage Utility provides a mechanism for storing significant amounts of textual data, client-side, whether or not your browsers supports the proposed HTML 5 Storage specification.
Each instance of the Storage Utility leverages one of three storage engines in order to store data:
document.localStorage
and document.sessionStorage
).Matt Snider, the lead frontend engineer for Mint.com (recently acquired by Intuit), contributed the YUI Storage Utility in the 2.8.0 release. In this session, he provides an overview of the Storage Utility's features and the nuances of the various storage backends.
The Storage Utility is dependant on the Yahoo Global Object, Dom, and Event. Optionally, if you wish to use session storage, then include Cookie, and if you plan to use SWF storage include SWF Store and its dependencies. In a common implementation that relied on both session storage and SWFStore, you would include the following source files in your web page:
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js" ></script> <script src="http://yui.yahooapis.com/2.9.0/build/cookie/cookie-min.js" ></script> <script src="http://yui.yahooapis.com/2.9.0/build/swf/swf-min.js" ></script> <script src="http://yui.yahooapis.com/2.9.0/build/storage/storage-min.js" ></script>
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js" ></script> <script src="http://yui.yahooapis.com/2.9.0/build/cookie/cookie-min.js" ></script> <script src="http://yui.yahooapis.com/2.9.0/build/swf/swf-min.js" ></script> <script src="http://yui.yahooapis.com/2.9.0/build/storage/storage-min.js" ></script>
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader to write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is storage
. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug
versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
With the Storage Utility present, you can now store large amounts of data on the client's machine. But first you must retrieve an engine instance by calling StorageManager.get
with the following optional arguments:
StorageEngineType.ENGINE_NAME
constants. Currently, you can choose from YAHOO.util.StorageEngineGears
, YAHOO.util.StorageEngineSWF
, and YAHOO.util.StorageEngineHTML5
.StorageManager.get
and/or the engines instantiated.A sample instantiation might look like this:
var storageEngine = YAHOO.util.StorageManager.get( YAHOO.util.StorageEngineHTML5.ENGINE_NAME, YAHOO.util.StorageManager.LOCATION_LOCAL, { force: false, order: [ YAHOO.util.StorageEngineGears, YAHOO.util.StorageEngineSWF ] }); //storageEngine will be an instance of YAHOO.util.Storage.
var storageEngine = YAHOO.util.StorageManager.get( YAHOO.util.StorageEngineHTML5.ENGINE_NAME, YAHOO.util.StorageManager.LOCATION_LOCAL, { force: false, order: [ YAHOO.util.StorageEngineGears, YAHOO.util.StorageEngineSWF ] }); //storageEngine will be an instance of YAHOO.util.Storage.
The following is the general code pattern used to fetch an engine, then write/read some data:
// this will fetch the first available engine var storageEngine = YAHOO.util.StorageManager.get(); storageEngine.subscribe(storageEngine.CE_READY, function() { storageEngine.setItem('testText', 'this is a triumph'); storageEngine.setItem('testNumber', 1234567890); storageEngine.setItem('testBoolean', true); alert(storageEngine.getItem('testText')); });
// this will fetch the first available engine var storageEngine = YAHOO.util.StorageManager.get(); storageEngine.subscribe(storageEngine.CE_READY, function() { storageEngine.setItem('testText', 'this is a triumph'); storageEngine.setItem('testNumber', 1234567890); storageEngine.setItem('testBoolean', true); alert(storageEngine.getItem('testText')); });
The Storage Utility is configured via the third argument to the get
method. This optional argument is an object containing one or more of the following fields:
Configuration Option | Purpose |
---|---|
force | (boolean) By default the StorageManager will attempt all engines until it finds one that works, but when force is true it will confine itself to the engine specified in the first argument. |
order | (array) By default the StorageManager will attempt engines in the order that they appear in the code. If you would like to change the order in which storage engines are tried, then add them to this option. This is an array of storage engine classes; the possible values are YAHOO.util.StorageEngineGears , YAHOO.util.StorageEngineSWF , and YAHOO.util.StorageEngineHTML5 .
|
engine | (object) The configuration object to pass into the engine. |
engine.swfURL | (string) For SWFStore, this is the URL of the swfstore.swf ; by default /swfstore.swf is used. |
engine.containerID | (string) For SWFStore, the container into which the SWF will be inserted; by default it is inserted into body element. |
engine.attributes | (object) For SWFStore, this object will be used by swf.js when creating the SWF object. |
Once a storage engine has been instantiated, the engine instance can be used to write and read data from the engine. Mostly string
s should be stored, but we have added meta data to allow the storing of number
, boolean
, or NULL
as well. Here's a summary of the methods available on the engine object:
Member | Contents |
---|---|
length | (number) The number of keys currently stored in the engine. |
clear() | (function) Clears all keys and the data from the engine. |
getItem(string) | (function) Fetches the data by a key. This method will return a string , number , boolean , or NULL dependending on what was provided to setItem . |
getName() | (string) Fetches the Storage Engine's name constant. |
hasKey(string) | (function) Evaluates if the provided key has a value. |
key(number) | (string) Fetches the key at a given index. |
removeKey(string) | (function) Removes the key and its value from the engine. |
setItem(string, mixed) | (function) Adds or updates the value for a given key. The following data types can be stored and returned: string , number , boolean , or NULL . |
Each storage engine has its own restrictions and limitations. The engine files themselves make note of these restrictions, but here is a summary:
CE_READY
custom event must be subscribed to before using engine;About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:
There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.
More Information:
The Storage Utility is fully supported on Apple's iPhone, which implements the HTML 5 storage proposal. We have not tested the Storage Utility against other devices in this class.
The YUI Library and related topics are discussed on the on the YUILibrary.com forums.
Also be sure to check out YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.
Copyright © 2013 Yahoo! Inc. All rights reserved.