@@ -368,6 +368,59 @@ app.on('init', function() {
368
368
369
369
Any JSON-serializable object can be set as a ` playOption ` .
370
370
371
+ ## SavedData
372
+ The SavedData API is made up of three classes: SavedData, SavedDataHandler, and the UserDataPlugin.
373
+ It allows the container (or the Springroll Application) to store key-value pairs in local or session storage. It is primarily
374
+ used to store user data for use across the Springroll environment. Examples are listed below for each class.
375
+
376
+ ### SavedData
377
+ The ` SavedData ` class is the most direct way to access the Container storage options. It is used primarily in plugin classes, but may
378
+ be used wherever necessary.
379
+
380
+ ``` javascript
381
+ import { SavedData } from ' springroll-container' ;
382
+
383
+ // the last argument, tempOnly, is optional (defaults to false) and decides whether the value should be saved in session (temporary),
384
+ // or local (not temporary) storage.
385
+ const tempOnly = false ;
386
+ SavedData .write (' user-value-key' , ' user-value' , tempOnly);
387
+
388
+ // SavedData.read() checks localStorage first and then sessionStorage. If the key exists in both only the localStorage will be returned.
389
+ let data = SavedData .read (' user-value-key' ); // data will be either the value in storage, if it exists, or null if it does not.
390
+
391
+ SavedData .remove (' user-value-key' ); // removes the value from both local and session storage.
392
+ ```
393
+
394
+ ### SavedDataHandler
395
+ The SavedDataHandler class is used primarily in the ` UserDataPlugin ` to interact with the ` SavedData ` class. But can be used directly
396
+ if you require a callback when reading or writing from ` SavedData ` . Like ` SavedData ` all of the methods are static.
397
+ ``` javascript
398
+ import { SavedDataHandler } from ' springroll-container' ;
399
+
400
+ SavedDataHandler .write (' user-value-name' , ' value-to-be-stored' , () => console .log (' user-value-name written to storage' ));
401
+
402
+ SavedDataHandler .read (' user-value-name' , value => console .log (' Returned value: ' + value));
403
+
404
+ SavedDataHandler .remove (' user-value-name' , () => console .log (' user-value-name removed from storage' ));
405
+
406
+ ```
407
+ We don't expect this handler to be used very often, but it is available if required.
408
+
409
+ ### UserDataPlugin
410
+ This plugin allows the container to respond to requests from the Springroll Application's [ User Data Class] ( https://github.com/SpringRoll/SpringRoll/tree/master/src/state ) .
411
+ It is included in the container constructor like any other plugin:
412
+ ``` javascript
413
+ import { UserDataPlugin , Container } from ' springroll-container' ;
414
+
415
+ const container = new springroll.Container (' #game' , {
416
+ plugins: [
417
+ new UserDataPlugin (),
418
+ ]
419
+ });
420
+ container .openPath (' game.html' );
421
+ ```
422
+ There is no configuration required for the UserDataPlugin as it just handles requests from the Application.
423
+
371
424
## Documentation
372
425
373
426
[ API Documentation] ( http://springroll.github.io/SpringRollContainer/ ) has full documentation for the Container.
0 commit comments