YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: SWFStore Utility: SWFStore Settings Example

# SWFStore Utility: SWFStore Settings Example

This example shows what can happen if storage capacity is exceeded, and how to deal with it effectively. By default, you're allowed 100k per domain. If a larger amount of data is attempted to be stored, an exception will occur. This shows how to deal with that exception and how to enable more storage.

View example in new window.

## Setting up the Example

The first step is to instantiate a SWFStore and set up listeners, once the page is ready. We also disable all of the Buttons at first.

function initialize() { var useCompression = compressionCheckbox.get("checked"); saveButton.set("disabled", true); purgeButton.set("disabled", true); compressionCheckbox.set("disabled", true); resizeButton.set("disabled", true); setsizeButton.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("contentReady", onContentReady); swfstore.addListener("inadequateDimensions", onInadequateDimensions); swfstore.addListener("securityError", onError); }

Once SWFStore is ready, it will dispatch a `contentReady` event. We can then enable the Buttons.

function onContentReady(event) { saveButton.set("disabled", false); purgeButton.set("disabled", false); compressionCheckbox.set("disabled", false); displayButton.set("disabled", false); setsizeButton.set("disabled", false); resizeButton.set("disabled", false); }

## Exceeding Storage Capacity

Unless the user has disabled storage altogether, or set it below the default, you will generally have 100kb per domain. If you expect that you'll need additional storage, you should call the `setSize` method in advance, passing in an amount greater than you expect to use. Don't worry about requesting too much, as it's only an allotted amount and no hard drive space will be taken up until it's filled with data. Plus, Flash Player is set up to round off values -- 0kb, 10kb, 100kb, 1mb, 10mb, and unlimited. For instance, if you expect to store no more than 300kb, you might as well ask for more as Flash Player's request dialog will actually ask for 1mb.

If the storage amount requested exceeds what is currently set as the maximum available storage, whether through calling `setSize(amount)` or by attempting to store an item that is larger than the available space, a `quotaExceededError` is dispatched and the item will not save. Flash Player will attempt to display a dialog requesting additional storage. However, since the SWF we've embedded is `0px` x `0px`, the dialog has insufficient space to display. We can detect this in advance via `swfstore.hasAdequateDimensions()`. The SWF itself will therefore detect whether it's large enough--if not, we can then simply resize the SWF's container and try again. Here we've done just that, using the Animation utility to make the resize more apparent. We check the size of the SWF first, and if it's not large enough we resize it and tell the Animation utility to call the function again once the resize is complete. This is necessary because the dialogs will not render if the SWF is too small--thus you cannot simply resize after invoking the dialog, you must check the SWF's dimensions in advance.

function setStorageAmount() { if(swfstore.hasAdequateDimensions()) { anim.onComplete.unsubscribe(setStorageAmount); swfstore.setSize(parseInt(YAHOO.util.Dom.get('storageAmount').value)); } else { //swf is not large enough to display the storage request panel, so resize it //have it try again when the animation is finished anim.onComplete.subscribe(setStorageAmount); resizeSWF(); } }

The settings panel can also be invoked manually. You can try this in this example by clicking the Display Settings button and adjusting the slider in the dialog that displays--or even turn off storage altogether. This is accomplished in code by calling `swfstore.displaySettings()`. Here we've done so in the `showSettings` function below. Again we've used the Animation utility to resize the SWF if necessary. It is set up to call `showSettings()` again once it's finished its animation, thereby ensuring display of the dialog. Note that the required space for these SWF dialogs is 215px wide by 138px high, but it's better to set it a bit higher due to CSS padding or other factors which may decrease its true dimensions. 300x300 is a nice starting point.

function showSettings() { if(swfstore.hasAdequateDimensions()) { anim.onComplete.unsubscribe(showSettings); swfstore.displaySettings(); } else { anim.onComplete.subscribe(showSettings); resizeSWF(); } }

## Avoiding the Problem Altogether

If you try storing a larger amount of data than the allotted storage available, such as trying to store the full text of Shakespeare's Hamlet, about 180kb, in the default 100kb of space, the save will fail as I've mentioned. Try copying and pasting that text and saving. A way to increase the amount of data you can store without requiring the dialogs is by compressing your data. This is accomplished via the `useCompression` parameter of SWFStore (off by default). Try turning on compression by clicking the Use Compression button. This should compress the 180kb down to about 70kb, and it should save properly. Be sure to purge out the data when you're done to avoid taking up precious hard drive space.

## 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.

### SWFStore Utility Examples:

- SWFStore Advanced Example
- SWFStore Settings Example

### More SWFStore Utility Resources:

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

Copyright © 2010 Yahoo! Inc. All rights reserved.

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