Skip to content

Instantly share code, notes, and snippets.

@miketierney
Last active December 17, 2015 23:28

Revisions

  1. miketierney revised this gist Jun 1, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion datastore.js
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@
    *
    */

    var dataStore = (function () {
    var DataStore = (function () {

    /**
    * Constructor. Takes a name and optional second value, which will be set in
  2. miketierney revised this gist Jun 1, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions datastore.js
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,18 @@

    var dataStore = (function () {

    /**
    * Constructor. Takes a name and optional second value, which will be set in
    * to the data store using the `set` method.
    *
    * @param name {String} string to use as the key for storing the data in
    * localStorage
    * @param data (optional) can be any valid JavaScript object, will get stored
    * in the `name`d location in localStorage
    **/
    var dataStore = function(name){
    this.name = name;
    if (data) { this.set(data); }
    };

    dataStore.prototype = {
  3. miketierney revised this gist Jun 1, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions datastore.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,11 @@
    * localStorage abstraction
    * Get and Set JS Objects in localStorage
    *
    * This is meant for really simple localStorage needs, not to replace
    * anything like Backbone.localStorage that's more robust. A good
    * use-case is storing an array of IDs for models that are stored on
    * the server.
    *
    * Usage:
    *
    * var myStore = new dataStore('storageName');
  4. miketierney revised this gist Jun 1, 2013. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions datastore.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,18 @@
    /**
    * localStorage abstraction
    * Get and Set JS Objects in localStorage
    *
    * Usage:
    *
    * var myStore = new dataStore('storageName');
    * // >> dataStore {name: "storageName", fetch: function, set: function}
    * myStore.set({someKey : "This will be stored as JSON"});
    * // >> undefined
    * myStore.fetch();
    * // >> Object {someKey : "This will be stored as JSON"}
    * localStorage.getItem('storageName');
    * // >> "{"someKey":"This will be stored as JSON"}"
    *
    */

    var dataStore = (function () {
  5. miketierney created this gist Jun 1, 2013.
    39 changes: 39 additions & 0 deletions datastore.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    /**
    * localStorage abstraction
    * Get and Set JS Objects in localStorage
    */

    var dataStore = (function () {

    var dataStore = function(name){
    this.name = name;
    };

    dataStore.prototype = {
    /**
    * Getter method; retrieves the data stored under the dataStore's name from
    * localStorage, then parses it with JSON.parse
    *
    * @method fetch
    * @param none
    * @returns object
    **/
    fetch : function () {
    return JSON.parse(localStorage.getItem(this.name));
    },

    /**
    * Setter method; takes a JavaScript object, runs it through JSON
    * stringify, and then stores it in localStorage under the dataStore's name
    *
    * @method set
    * @param obj {Object} Can be any valid JavaScript object -- whether that's an object or a string. Should be something that can be parsed by JSON.stringify
    * @returns undefined
    **/
    set : function (obj) {
    localStorage.setItem(this.name, JSON.stringify(obj));
    }
    };

    return dataStore;
    })();