This example demonstrates a rudimentary use case for the Storage Utility. When you enter text in the text field below, your content will be locally saved on every fifth keypress
. If your browser supports one of the Storage Utility's storage mechanisms (Gears, SWF, and HTML5 are tried, in that order), your last-saved content will persist when you reload the page.
Watch the logger on the right side of the page as you type for feedback about what the example is doing.
This example shows Storage being used in a simple context. The full source for the example follows. Of particular interest here is the instantiation of Storage, wrapped in a try/catch statement, where the catch logic handles the logic for browsers that don't support one of your requested storage mechanisms. Because we are allowing SWF storage here, we also wrap the followup logic — the logic that interacts with the fully instantiated Storage object — in the CE_READY
custom event, ensuring that we are not trying to interact with the object before it's fully created. (SWFStore has a slight asynchronous delay in instantiation; that's why it's important that we listen for this event when SWF is one of our possible storage mechanisms.)
In this example, we specify Gears as our preferred storage mechanism and we provide the order in which we want Storage to progress if Gears isn't present — from Gears to SWF to HTML5.
Note that before the main code block below we have included the gears_init.js
file to prepare the page for use of Gears:
1 | <script type="text/javascript" src="../storage/assets/gears_init.js"></script> |
view plain | print | ? |
Here is the main JS codeblock that drives this example, along with some inline comments to explain what's being done and why:
1 | YAHOO.util.Event.onDOMReady(function() { |
2 | |
3 | var ctr = 0, |
4 | storageEngine; |
5 | |
6 | YAHOO.util.StorageEngineSWF.SWFURL = 'swfstore.swf'; |
7 | |
8 | //wrap instantiation in a try/catch to handle situations where |
9 | //the client browser does not support any of your desired |
10 | //storage engine; use the catch statement to handle those |
11 | //unsupported browsers. In this case, we're using Storage as |
12 | //an enhancement, and we can fail gracefully if instantiation |
13 | //fails. |
14 | try { |
15 | storageEngine = YAHOO.util.StorageManager.get( |
16 | YAHOO.util.StorageEngineGears.ENGINE_NAME, |
17 | YAHOO.util.StorageManager.LOCATION_LOCAL, |
18 | { |
19 | order: [ |
20 | YAHOO.util.StorageEngineGears, |
21 | YAHOO.util.StorageEngineSWF, |
22 | YAHOO.util.StorageEngineHTML5 |
23 | ], |
24 | force: false |
25 | } |
26 | ); |
27 | } catch(e) { |
28 | YAHOO.log("No supported storage mechanism present."); |
29 | storageEngine = false; |
30 | } |
31 | |
32 | //storageEngine will be truthy if instantiation succeeded and |
33 | //false if no engine was available or if instantiation failed |
34 | //for any reason. |
35 | if(storageEngine) { |
36 | |
37 | //If we got a valid storage egnine, we can make use of it. |
38 | //Use the custom event CE_READY to identify the moment |
39 | storageEngine.subscribe(storageEngine.CE_READY, function(e) { |
40 | YAHOO.log("A Storage instance is ready using " + |
41 | storageEngine.getName() + |
42 | "; start typing in the text box and content will be saved every five keystrokes.", "info", "example"); |
43 | |
44 | //Prepopulate the text field with saved contents, if present. |
45 | if(storageEngine.hasKey("simple-storage-textentry")) { |
46 | YAHOO.log(storageEngine.getItem("simple-storage-textentry")); |
47 | YAHOO.util.Dom.get("textentry").value = storageEngine.getItem("simple-storage-textentry"); |
48 | } |
49 | |
50 | //Set up an event listener to trigger the Storage set method |
51 | //every five keystrokes. |
52 | YAHOO.util.Event.on("textentry", "keypress", function(e) { |
53 | ctr++; |
54 | |
55 | if(!(ctr%5)) { //on every fifth keystroke, autosave the contents |
56 | storageEngine.setItem("simple-storage-textentry", YAHOO.util.Dom.get("textentry").value); |
57 | document.getElementById("autosave").innerHTML = "last saved at " + (new Date).toString(); |
58 | YAHOO.log("Autosaved textarea content to " + |
59 | storageEngine.getName() + " via Storage Utility.", "info", "example"); |
60 | } |
61 | }); |
62 | }); |
63 | } |
64 | |
65 | }); |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 250ms (+134) 5:16:26 AM:
LogReader instance0
LogReader initialized
INFO 116ms (+1) 5:16:26 AM:
Get
Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 115ms (+0) 5:16:26 AM:
Get
attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 115ms (+115) 5:16:26 AM:
Get
_next: q0, loaded: undefined
INFO 0ms (+0) 5:16:26 AM:
global
Logger initialized
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings