Source: core/Store.js

// Generated by github.com/steida/coffee2closure 0.1.12
goog.provide('spark.core.Store');
goog.require('spark.core.Object');
goog.require('spark.validation');
goog.require('goog.structs.Map');
goog.require('goog.string');

/**

  @constructor
  @export
  @param   {Object=} options Class options.
  @param   {*=} data Class data
  @extends {spark.core.Object}
 */
spark.core.Store = function(options, data) {
  var _ref;
  if (options == null) {
    options = {};
  }
  options.validations || (options.validations = options['validations'] || {});
  if (options.validateOnInitialize == null) {
    options.validateOnInitialize = (_ref = options['validateOnInitialize']) != null ? _ref : true;
  }
  spark.core.Store.superClass_.constructor.call(this, options, data);
  this.map_ = new goog.structs.Map(data);
  if (options.validateOnInitialize) {
    this.validateAll();
  }
}
goog.inherits(spark.core.Store, spark.core.Object);

/**
  Returns value of the given key from the store. String values will be encoded
  by default to prevent possible XSS attacks. If you don't want the encoding
  for some reason pass a second `false` parameter.

  @export
  @param {string} key Key to be returned.
  @param {boolean=} escape Whether you want to espace the value or not.
  @return {*}
 */
spark.core.Store.prototype.get = function(key, escape) {
  var value;
  if (escape == null) {
    escape = true;
  }
  value = this.map_.get(key);
  if (escape && spark.validation.isString(value)) {
    value = goog.string.htmlEscape(value);
  }
  return value;
};

/**
  Sets a new key value pair into store. It will also make a validation before
  setting the new value. If you set a validation rule in options.validations
  for that property, when validation failed, the property won't be updated.
  Store::set will return no and emit a 'PropertyRejected' event otherwise
  it will return true and it will also emit 'ProperySet'. Both events will
  include key and value. If the key and value passed to set does not related
  with a validation rule, it will directly set to store. This makes validation
  optional.

  @export
  @param {string} key Key to be returned.
  @param {*} value Value of the key.
  @return {boolean} Whether the set is completed or not.
 */
spark.core.Store.prototype.set = function(key, value) {
  var isValid;
  if (!key || (value == null)) {
    return false;
  }
  isValid = true;
  if (this.validate(key, value)) {
    this.map_.set(key, value);
    this.emit('PropertySet', {
      key: key,
      value: value
    });
  } else {
    this.emit('PropertyRejected', {
      key: key,
      value: value
    });
    isValid = false;
  }
  return isValid;
};

/**
  Validates the given value for given key's validation rules.

  @export
  @param {string} key Key to be validated.
  @param {*} value Value of the key to validate.
  @return {boolean} Whether the validation is passed or failed.
 */
spark.core.Store.prototype.validate = function(key, value) {
  var isValid, method, rule, rules, type, validations;
  validations = this.getOptions().validations;
  rules = validations != null ? validations[key] : void 0;
  isValid = true;
  if (!rules) {
    return true;
  }
  for (type in rules) {
    rule = rules[type];
    method = spark.validation.getValidator(type, rule);
    if (!method) {
      throw new Error("Validation type " + type + " does not exist.");
    }
    isValid = method.call(this, value, rule);
    if (!isValid) {
      break;
    }
  }
  return isValid;
};

/**
  Validates all data and throw an error if validation fails.

  @export
 */
spark.core.Store.prototype.validateAll = function() {
  var key, value, _ref;
  _ref = this.getData();
  for (key in _ref) {
    value = _ref[key];
    if (!this.validate(key, value)) {
      throw new Error("Failed to validate store data, " + key + ": " + value);
    }
  }
  return true;
};

/**
  Removes a key from strore.

  @export
  @param {string} key Key to be removed.
  @return {boolean} Whether the key removed or not.
 */
spark.core.Store.prototype.unset = function(key) {
  return this.map_.remove(key);
};

/**
  Removes all keys from store.

  @export
 */
spark.core.Store.prototype.clear = function() {
  return this.map_.clear();
};

/**
  Returns `true` if the store has the given key.

  @export
  @param {string} key Key to be searched in store.
  @return {boolean} Whether the store has the key or not.
 */
spark.core.Store.prototype.has = function(key) {
  return this.map_.containsKey(key);
};

/**
  Returns keys array.

  @export
  @return {Array} Keys array.
 */
spark.core.Store.prototype.getKeys = function() {
  return this.map_.getKeys();
};

/**
  Returns values array.

  @export
  @return {Array} Values array.
 */
spark.core.Store.prototype.getValues = function() {
  return this.map_.getValues();
};

/**
  Returns the current data as object.

  @export
  @return {Object} Store data.
 */
spark.core.Store.prototype.toObject = function() {
  return this.map_.toObject();
};
Spark Framework by Fatih Acet
Copyright © 2014 - Fatih Acet
Documentation generated by JSDoc 3.2.2 on 2015-07-19T22:09:29+00:00 using the DocStrap template.