not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

  1. define(["sugar-web/activity/activity", "sugar-web/env"], function (activity, env) {
  2. 'use strict';
  3. // This is a helper module that allows to persist key/value data
  4. // using the standard localStorage object.
  5. //
  6. // Usage:
  7. // ------
  8. //
  9. // // 1. Setup:
  10. //
  11. // dictstore.init(onReadyCallback);
  12. //
  13. // // 2. Use localStorage directly, and then call save():
  14. //
  15. // var value = localStorage['key'];
  16. // localStorage['key'] = newValue;
  17. // dictstore.save(onSavedCallback);
  18. //
  19. var dictstore = {};
  20. dictstore.init = function (callback) {
  21. if (env.isStandalone()) {
  22. // In standalone mode, use localStorage as is.
  23. callback();
  24. } else {
  25. // In Sugar, set localStorage from the datastore.
  26. localStorage.clear();
  27. var onLoaded = function (error, metadata, jsonData) {
  28. var data = JSON.parse(jsonData);
  29. for (var i in data) {
  30. localStorage[i] = data[i];
  31. }
  32. callback();
  33. };
  34. activity.getDatastoreObject().loadAsText(onLoaded);
  35. }
  36. };
  37. // Internally, the key/values are stored as text in the Sugar
  38. // datastore, using the JSON format.
  39. dictstore.save = function (callback) {
  40. if (callback === undefined) {
  41. callback = function () {};
  42. }
  43. if (env.isStandalone()) {
  44. // In standalone mode, use localStorage as is.
  45. callback();
  46. } else {
  47. var datastoreObject = activity.getDatastoreObject();
  48. var jsonData = JSON.stringify(localStorage);
  49. datastoreObject.setDataAsText(jsonData);
  50. datastoreObject.save(function (error) {
  51. callback(error);
  52. });
  53. }
  54. };
  55. return dictstore;
  56. });