Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
sugar-web/dictstore.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
68 lines (56 sloc)
1.83 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(["sugar-web/activity/activity", "sugar-web/env"], function (activity, env) { | |
| 'use strict'; | |
| // This is a helper module that allows to persist key/value data | |
| // using the standard localStorage object. | |
| // | |
| // Usage: | |
| // ------ | |
| // | |
| // // 1. Setup: | |
| // | |
| // dictstore.init(onReadyCallback); | |
| // | |
| // // 2. Use localStorage directly, and then call save(): | |
| // | |
| // var value = localStorage['key']; | |
| // localStorage['key'] = newValue; | |
| // dictstore.save(onSavedCallback); | |
| // | |
| var dictstore = {}; | |
| dictstore.init = function (callback) { | |
| if (env.isStandalone()) { | |
| // In standalone mode, use localStorage as is. | |
| callback(); | |
| } else { | |
| // In Sugar, set localStorage from the datastore. | |
| localStorage.clear(); | |
| var onLoaded = function (error, metadata, jsonData) { | |
| var data = JSON.parse(jsonData); | |
| for (var i in data) { | |
| localStorage[i] = data[i]; | |
| } | |
| callback(); | |
| }; | |
| activity.getDatastoreObject().loadAsText(onLoaded); | |
| } | |
| }; | |
| // Internally, the key/values are stored as text in the Sugar | |
| // datastore, using the JSON format. | |
| dictstore.save = function (callback) { | |
| if (callback === undefined) { | |
| callback = function () {}; | |
| } | |
| if (env.isStandalone()) { | |
| // In standalone mode, use localStorage as is. | |
| callback(); | |
| } else { | |
| var datastoreObject = activity.getDatastoreObject(); | |
| var jsonData = JSON.stringify(localStorage); | |
| datastoreObject.setDataAsText(jsonData); | |
| datastoreObject.save(function (error) { | |
| callback(error); | |
| }); | |
| } | |
| }; | |
| return dictstore; | |
| }); |