Source: core/RemoteStore.js

// Generated by github.com/steida/coffee2closure 0.1.12
goog.provide('spark.core.RemoteStore');
goog.require('spark.core.Store');
goog.require('spark.validation');
goog.require('spark.ajax');

/**
  RemoteStore implementation of the Spark Framework. Makes HTTP GET, POST
  PUT or DELETE request to manage the data on the server. Use fetch to get or
  update the data, use save to write data back to server or remove to delete
  the data from the server.

  @constructor
  @export
  @param   {Object=} options Class options.
  @param   {*=} data Class data
  @extends {spark.core.Store}
 */
spark.core.RemoteStore = function(options, data) {
  if (options == null) {
    options = {};
  }
  options.url || (options.url = options['url'] || null);
  options.idKey || (options.idKey = options['idKey'] || '_id');
  spark.core.RemoteStore.superClass_.constructor.call(this, options, data);
}
goog.inherits(spark.core.RemoteStore, spark.core.Store);

/**
  Saves the data to server or updates it on the server. If your data has
  `options.idKey` property it will make a PUT request to update the data
  otherwise it will make a `POST` request to save it to server. If server
  response includes an `options.idKey` RemoteStore will save the id and use
  it to update the data.

  @export
  @param {Function=} callback Callback function to handle the request.
 */
spark.core.RemoteStore.prototype.save = function(callback) {
  var POST, PUT, idKey, method, object, _ref;
  _ref = spark.ajax.RequestTypes, PUT = _ref.PUT, POST = _ref.POST;
  idKey = this.getOptions().idKey;
  method = this.get(idKey) ? PUT : POST;
  object = this.toObject();
  delete object[idKey];
  return spark.ajax.request({
    type: method,
    url: this.getUrl_(method),
    data: object,
    success: (function(_this) {
      return function(data) {
        if (method === 'POST') {
          _this.set(idKey, data[idKey]);
        }
        _this.emit(spark.core.RemoteStore.EventTypes.SAVED, data);
        if (callback) {
          return callback.call(_this, null, data);
        }
      };
    })(this),
    error: (function(_this) {
      return function(err) {
        _this.emit(spark.core.RemoteStore.EventTypes.SAVE_FAILED, err);
        if (callback) {
          return callback.call(_this, err, null);
        }
      };
    })(this)
  });
};

/**
  Fetches the store data from server. Uses idKey property to find the store
  on the server. You can use fetch to populate the store from the server or
  when you want to update store data with latest data on the server.
  Callback is optional. Depending the result `EventTypes.FETCHED` or
  `EventTypes.FETCH_FAILED` events will be triggered.

  @export
  @param {Function=} callback Callback function to handle the request.
 */
spark.core.RemoteStore.prototype.fetch = function(callback) {
  var method;
  method = spark.ajax.RequestTypes.GET;
  return spark.ajax.request({
    type: method,
    url: this.getUrl_(method),
    success: (function(_this) {
      return function(data) {
        var key, value;
        for (key in data) {
          value = data[key];
          _this.set(key, value);
        }
        _this.emit(spark.core.RemoteStore.EventTypes.FETCHED, data);
        if (callback) {
          return callback.call(_this, null, data);
        }
      };
    })(this),
    error: (function(_this) {
      return function(err) {
        _this.emit(spark.core.RemoteStore.EventTypes.FETCH_FAILED, err);
        if (callback) {
          return callback.call(_this, err, null);
        }
      };
    })(this)
  });
};

/**
  Removes a store on the server by making a HTTP DELETE request. Handle the
  response with a callback or emitted events. Depending the response
  `EventTypes.REMOVED` or `EventTypes.REMOVE_FAILED` events will be emitted.

  @export
  @param {Function=} callback Callback function to handle the request.
 */
spark.core.RemoteStore.prototype.remove = function(callback) {
  var method;
  method = spark.ajax.RequestTypes.DELETE;
  return spark.ajax.request({
    type: method,
    url: this.getUrl_(method),
    success: (function(_this) {
      return function(data) {
        _this.emit(spark.core.RemoteStore.EventTypes.REMOVED, data);
        if (callback) {
          return callback.call(_this, null, data);
        }
      };
    })(this),
    error: (function(_this) {
      return function(err) {
        _this.emit(spark.core.RemoteStore.EventTypes.REMOVE_FAILED, err);
        if (callback) {
          return callback.call(_this, err, null);
        }
      };
    })(this)
  });
};

/**
  This method allows RemoteStore to work with customized urls for different
  request types. If the `url` in `options` is string then RemoteStore will
  use it and don't touch it. It may also be a function which will return
  different urls for different request types. See the following example.

  ```coffee
    new spark.core.RemoteStore
      url: (type) ->
        if type is 'GET'
          return '/user/list'
        else if type is 'POST'
          return '/user'
  ```

  @private
  @param {string} method Request type to customize request url.
  @return {string} The request url.
 */
spark.core.RemoteStore.prototype.getUrl_ = function(method) {
  var url;
  url = this.getOptions().url;
  if (spark.validation.isFunction(url)) {
    url = url.call(this, method);
  }
  return url;
};

/**
  Emitted event types.

  @enum {string}
 */
spark.core.RemoteStore.EventTypes = {
  SAVED: 'Saved',
  SAVE_FAILED: 'SaveFailed',
  FETCHED: 'Fetched',
  FETCH_FAILED: 'FetchFailed',
  REMOVED: 'Removed',
  REMOVE_FAILED: 'RemoveFailed'
};
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.