este-library-oldschool
Version:
Library for github.com/steida/este.git
160 lines (149 loc) • 4.24 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
/**
@fileoverview Local storage for este.Model's via HTML5 or IE user data.
@see /demos/storage/local.html
*/
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.superClass_ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
goog.provide('este.storage.Local');
goog.require('este.json');
goog.require('goog.result');
goog.require('este.storage.Base');
goog.require('goog.object');
goog.require('goog.storage.mechanism.mechanismfactory');
goog.require('goog.string');
goog.require('goog.uri.utils');
/**
@param {string} namespace
@param {string|number=} version
@param {goog.storage.mechanism.Mechanism=} mechanism
@param {function():string=} idFactory
@constructor
@extends {este.storage.Base}
*/
este.storage.Local = function(namespace, version, mechanism, idFactory) {
este.storage.Local.superClass_.constructor.call(this, namespace, version);
this.namespace = goog.uri.utils.appendPath(this.namespace, this.version);
this.mechanism = mechanism != null ? mechanism : goog.storage.mechanism.mechanismfactory.create(this.namespace);
this.idFactory = idFactory != null ? idFactory : goog.string.getRandomString;
}
extend(este.storage.Local, superClass);
/**
@type {goog.storage.mechanism.Mechanism}
@protected
*/
este.storage.Local.prototype.mechanism = null;
/**
@type {function():string}
@protected
*/
este.storage.Local.prototype.idFactory = function() {};
/**
@override
*/
este.storage.Local.prototype.addInternal = function(model, url) {
return this.saveInternal(model, url);
};
/**
@override
*/
este.storage.Local.prototype.loadInternal = function(model, url) {
var id, json, models;
id = model.getId();
models = this.loadModels(url);
if (!models) {
return goog.result.failedResult();
}
json = models[id];
if (!json) {
return goog.result.failedResult();
}
model.set(json);
return goog.result.successfulResult(model);
};
/**
@override
*/
este.storage.Local.prototype.saveInternal = function(model, url) {
var id, models, serializedModels;
this.ensureModelId(model);
id = model.getId();
serializedModels = this.mechanism.get(url);
models = serializedModels ? este.json.parse(serializedModels) : {};
models[id] = model.toJson(true);
this.saveModels(models, url);
return goog.result.successfulResult(model);
};
/**
@override
*/
este.storage.Local.prototype.removeInternal = function(model, url) {
var id, models;
id = model.getId();
if (id) {
models = this.loadModels(url);
if (models && models[id]) {
delete models[id];
this.saveModels(models, url);
return goog.result.successfulResult(model);
}
}
return goog.result.failedResult();
};
/**
@override
*/
este.storage.Local.prototype.queryInternal = function(collection, url, params) {
var array, id, model, models;
models = this.loadModels(url);
array = (function() {
var results;
results = [];
for (id in models) {
model = models[id];
results.push(model);
}
return results;
})();
collection.reset(array);
return goog.result.successfulResult(collection);
};
/**
@param {este.Model} model
@protected
*/
este.storage.Local.prototype.ensureModelId = function(model) {
var id;
id = model.getId();
if (id) {
return;
}
return model.setId(this.idFactory());
};
/**
@param {Object.<string, Object>} models
@param {string} url
@protected
*/
este.storage.Local.prototype.saveModels = function(models, url) {
var serializedJson;
if (goog.object.isEmpty(models)) {
return this.mechanism.remove(url);
} else {
serializedJson = este.json.stringify(models);
return this.mechanism.set(url, serializedJson);
}
};
/**
@param {string} url
@return {Object.<string, Object>}
@protected
*/
este.storage.Local.prototype.loadModels = function(url) {
var serializedJson;
serializedJson = this.mechanism.get(url);
if (!serializedJson) {
return null;
}
return este.json.parse(serializedJson);
};