YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: SWFStore Utility: SWFStore Advanced Example

# SWFStore Utility: SWFStore Advanced Example

This example shows how to get, set, and remove items from storage using the YUI SWFStore Utility and how to display stored data using a YUI DataTable.

View example in new window.

Unable to load Flash content. The YUI File SWFStore Control requires Flash Player 9.0.115 or higher. You can download the latest version of Flash Player from the Adobe Flash Player Download Center.
Text to store:
Storage name:
Additional Option:

## Advanced SWFStore Example

This example consists of two text fields, a few Buttons and a DataTable. The Buttons allow you to save data, set properties on SWFStore, or clear all items from storage.

Once the page is drawn, `initialize` is called to instantiate a SWFStore instance and set up some listeners. We also disable all of the Buttons until the SWFStore is ready.

function initialize() { var useCompression = compressionCheckbox.get("checked"); saveButton.set("disabled", true); purgeButton.set("disabled", true); removeButton.set("disabled", true); removeAtButton.set("disabled", true); sharedataCheckbox.set("disabled", true); compressionCheckbox.set("disabled", true); //the swfstore instance swfstore = new YAHOO.util.SWFStore("swfstoreContainer", false, useCompression); //some basic listeners for user feedback swfstore.addListener("save", onSave); swfstore.addListener("error", onError); swfstore.addListener("quotaExceededError", onError); swfstore.addListener("securityError", onError); swfstore.addListener("contentReady", onContentReady); }

When SWFStore is ready, it will dispatch a `contentReady` event. We can then enable the Buttons and initialize a DataTable with any previously stored values. Because items are stored as objects, we need to loop through them to turn them into name-value pairs suitable for use with the DataTable.

function onContentReady(event) { saveButton.set("disabled", false); purgeButton.set("disabled", false); removeButton.set("disabled", false); removeAtButton.set("disabled", false); sharedataCheckbox.set("disabled", false); compressionCheckbox.set("disabled", false); load(); } function load() { //could use swfstore.getItems(), but that would not separate the data into fields var len = swfstore.getLength(); var arr = []; for (var i = 0; i < len; i++) { arr.push({ name:swfstore.getNameAt(i), value: swfstore.getValueAt(i) }) } var datasource = new YAHOO.util.LocalDataSource(arr); datasource.responseSchema = {fields : ["name", "value"]}; var configs = { scrollable: true } var columns = [ {key:"name", label:"Storage Name (Key)"}, {key:"value", label:"Text Stored"} ]; datatable = new YAHOO.widget.DataTable("datatableContainer", columns, datasource, configs); }

The "Save" Button is set up to take the values from the text fields and store them, using the `setItem` method of SWFStore.

function save() { swfstore.setItem( YAHOO.util.Dom.get('nameField').value, YAHOO.util.Dom.get('valueField').value); }

Similarly, a couple of other Buttons are set up to remove items from storage, either by name or index, or by clearing the entire local store.

function remove() { var obj = YAHOO.util.Dom.get('nameField').value; swfstore.removeItem(obj); } function removeItemAt() { var index = parseInt(YAHOO.util.Dom.get('indexField').value); swfstore.removeItemAt(index); } function purge() { swfstore.clear(); }

We've already set up a listener for a "save" event, which is dispatched once an item is successfully stored. That calls the `onSave` function, which loops through the rows of the DataTable. Using the `info` property of the event, we can determine what type of change occurred, and either add rows, delete rows, or update its current values.

function onSave(event) { //added if(event.info == "add" || event.info == "update") { var newobj = {name: event.key, value: event.newValue}; var len = datatable.getRecordSet().getLength(); //loop through current records and see if this has been added before for (var i = 0; i < len; i++ ) { var rec = datatable.getRecord(i); var data = rec.getData(); //if it's been added already, update it if(data.name == event.key) { datatable.updateRow(i, newobj); return; } } //if it's not been added, add it datatable.addRow(newobj); } //removed else if(event.info == "delete") { //var index = parseInt(YAHOO.util.Dom.get('indexField').value); datatable.deleteRow(event.index); } //cleared else { datatable.deleteRows(0, datatable.getRecordSet().getLength()); } }

## Configuration for This Example

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.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### SWFStore Utility Examples:

- SWFStore Advanced Example
- SWFStore Settings Example

### More SWFStore Utility Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings