[5283] | 1 | <!DOCTYPE html> |
---|
| 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
---|
| 3 | <head> |
---|
| 4 | <meta charset="utf-8" /> |
---|
| 5 | <meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
---|
| 6 | |
---|
| 7 | <title>$.store jQuery plugin</title> |
---|
| 8 | <meta name="title" content="$.store() jQuery plugin" /> |
---|
| 9 | <meta name="description" content="Persistently storing data on the client side" /> |
---|
| 10 | |
---|
| 11 | <script type="text/javascript" src="json2.js"></script> |
---|
| 12 | <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> |
---|
| 13 | <script type="text/javascript" src="jquery.store.js"></script> |
---|
| 14 | <script type="text/javascript"> |
---|
| 15 | (function($,undefined){ |
---|
| 16 | |
---|
| 17 | // initialize storage API |
---|
| 18 | $.storage = new $.store(); |
---|
| 19 | |
---|
| 20 | // build some demo interface |
---|
| 21 | var $buttons; |
---|
| 22 | |
---|
| 23 | var log = function( m ) |
---|
| 24 | { |
---|
| 25 | $('<p>'+ m +'</p>').insertAfter( $buttons ); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | var poll = function() |
---|
| 29 | { |
---|
| 30 | log( $.storage.get( 'event' ) ); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | var push = function() |
---|
| 34 | { |
---|
| 35 | $.storage.set( 'event', new Date() ); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | $(function() |
---|
| 39 | { |
---|
| 40 | $buttons = $( '<div></div>' ).prependTo( $( document.body ) ); |
---|
| 41 | |
---|
| 42 | $( '<button type="button">start writing</button>' ).appendTo( $buttons ).bind( 'click', function() |
---|
| 43 | { |
---|
| 44 | window.setInterval( push, 500 ); |
---|
| 45 | }); |
---|
| 46 | |
---|
| 47 | $( '<button type="button">start reading</button>' ).appendTo( $buttons ).bind( 'click', function() |
---|
| 48 | { |
---|
| 49 | window.setInterval( poll, 1000 ); |
---|
| 50 | }); |
---|
| 51 | }); |
---|
| 52 | |
---|
| 53 | })(jQuery); |
---|
| 54 | </script> |
---|
| 55 | <script type="text/javascript" src="jquery.windowSync.js"></script> |
---|
| 56 | </head> |
---|
| 57 | <body> |
---|
| 58 | |
---|
| 59 | <script type="text/javascript"> |
---|
| 60 | document.write( '<p>Initialized $.storage with driver ' + $.storage.driver.ident + '</p>' ); |
---|
| 61 | </script> |
---|
| 62 | |
---|
| 63 | <h1>$.store</h1> |
---|
| 64 | |
---|
| 65 | <p> |
---|
| 66 | <code>$.store</code> is a simple, yet easily extensible, plugin to persistently store data on the client side of things. |
---|
| 67 | It uses <code>window.localStore</code> where available. Older Internet Explorers will use <code>userData</code>. |
---|
| 68 | If all fails <code>$.store</code> will save your data to <code>window.name</code>. |
---|
| 69 | </p> |
---|
| 70 | |
---|
| 71 | <p> |
---|
| 72 | Note: The <code>windowName</code> will only do JSON serialization. <code>windowName</code> |
---|
| 73 | is not persistent in the sense of making it accross a closed browser window. If you need |
---|
| 74 | that ability you should check <code>$.storage.driver.scope == "browser"</code>. |
---|
| 75 | </p> |
---|
| 76 | |
---|
| 77 | <h2>Usage</h2> |
---|
| 78 | |
---|
| 79 | <pre>//initialize |
---|
| 80 | $.storage = new $.store(); |
---|
| 81 | |
---|
| 82 | // save a value |
---|
| 83 | $.storage.set( key, value ); |
---|
| 84 | |
---|
| 85 | // read a value |
---|
| 86 | $.storage.get( key ); |
---|
| 87 | |
---|
| 88 | // deletes a value |
---|
| 89 | $.storage.del( key ); |
---|
| 90 | |
---|
| 91 | // delete all values |
---|
| 92 | $.storage.flush();</pre> |
---|
| 93 | |
---|
| 94 | <h2>Adding Serializers</h2> |
---|
| 95 | |
---|
| 96 | <p> |
---|
| 97 | You can easily add your own serializers to the stack. |
---|
| 98 | Make sure to add them before initializing the <code>$.store</code>. |
---|
| 99 | Note: Serializers do not apply to the <code>windowName</code> storage driver. |
---|
| 100 | </p> |
---|
| 101 | <pre>$.store.serializers.yaddayadda = { |
---|
| 102 | ident: "$.store.serializers.yaddayadda", |
---|
| 103 | init: function( encoders, decoders ) |
---|
| 104 | { |
---|
| 105 | // register your serializer with en/decoder stack |
---|
| 106 | encoders.unshift( "yaddayadda" ); |
---|
| 107 | decoders.push( "yaddayadda" ); |
---|
| 108 | }, |
---|
| 109 | |
---|
| 110 | isYaddaYadda: function( value ) |
---|
| 111 | { |
---|
| 112 | // determine if value should be processed by this serializer |
---|
| 113 | return true; |
---|
| 114 | }, |
---|
| 115 | |
---|
| 116 | encode: function( value ) |
---|
| 117 | { |
---|
| 118 | // check if the value can be encoded |
---|
| 119 | if( !value || value._serialized || !this.isYaddaYadda( value ) ) |
---|
| 120 | return value; |
---|
| 121 | |
---|
| 122 | // prepare serialized-data-wrapper |
---|
| 123 | var _value = { _serialized: this.ident, value: value }; |
---|
| 124 | |
---|
| 125 | // work your magic |
---|
| 126 | _value.value = "serializedVersionOf data"; |
---|
| 127 | |
---|
| 128 | return value; |
---|
| 129 | }, |
---|
| 130 | |
---|
| 131 | decode: function( value ) |
---|
| 132 | { |
---|
| 133 | // check if the value can be decoded |
---|
| 134 | if( !value || !value._serialized || value._serialized != this.ident ) |
---|
| 135 | return value; |
---|
| 136 | |
---|
| 137 | // work your magic |
---|
| 138 | value.value = "unserializedVersionOf data"; |
---|
| 139 | |
---|
| 140 | return this.isYaddaYadda( value.value ) ? value.value : undefined; |
---|
| 141 | } |
---|
| 142 | };</pre> |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | </body> |
---|
| 146 | </html> |
---|