Sha256: 5aa4eca85b5713ec0b375f546c5006f1c8474ea984d0dc040099d0509bc0aa97

Contents?: true

Size: 1.95 KB

Versions: 30

Compression:

Stored size: 1.95 KB

Contents

(function() {

  Luca.LocalStore = (function() {

    function LocalStore(name) {
      var store;
      this.name = name;
      store = localStorage.getItem(this.name);
      this.data = (store && JSON.parse(store)) || {};
    }

    LocalStore.prototype.guid = function() {
      var S4;
      S4 = function() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
      };
      return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
    };

    LocalStore.prototype.save = function() {
      return localStorage.setItem(this.name, JSON.stringify(this.data));
    };

    LocalStore.prototype.create = function(model) {
      if (!model.id) model.id = model.attribtues.id = this.guid();
      this.data[model.id] = model;
      this.save();
      return model;
    };

    LocalStore.prototype.update = function(model) {
      this.data[model.id] = model;
      this.save();
      return model;
    };

    LocalStore.prototype.find = function(model) {
      return this.data[model.id];
    };

    LocalStore.prototype.findAll = function() {
      return _.values(this.data);
    };

    LocalStore.prototype.destroy = function(model) {
      delete this.data[model.id];
      this.save();
      return model;
    };

    return LocalStore;

  })();

  Backbone.LocalSync = function(method, model, options) {
    var resp, store;
    store = model.localStorage || model.collection.localStorage;
    resp = (function() {
      switch (method) {
        case "read":
          if (model.id) {
            return store.find(model);
          } else {
            return store.findAll();
          }
        case "create":
          return store.create(model);
        case "update":
          return store.update(model);
        case "delete":
          return store.destroy(model);
      }
    })();
    if (resp) {
      return options.success(resp);
    } else {
      return options.error("Record not found");
    }
  };

}).call(this);

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
luca-0.9.76 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.7 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.65 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.6 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.42 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.4 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.2 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.1 vendor/assets/luca-ui/modules/local_storage.js
luca-0.9.0 vendor/assets/luca-ui/modules/local_storage.js
luca-0.8.599 vendor/assets/luca-ui/modules/local_storage.js
luca-0.8.59 vendor/assets/luca-ui/modules/local_storage.js
luca-0.8.3 vendor/assets/luca-ui/modules/local_storage.js
luca-0.8.2 vendor/assets/luca-ui/modules/local_storage.js
luca-0.8.06 vendor/assets/luca-ui/modules/local_storage.js
luca-0.8 vendor/assets/luca-ui/modules/local_storage.js
luca-0.7.92 vendor/assets/luca-ui/modules/local_storage.js
luca-0.7.91 vendor/assets/luca-ui/modules/local_storage.js
luca-0.7.9 vendor/assets/luca-ui/modules/local_storage.js
luca-0.7.8 vendor/assets/luca-ui/modules/local_storage.js
luca-0.7.7 vendor/assets/luca-ui/modules/local_storage.js