Skip to content
avoidwork edited this page May 16, 2012 · 5 revisions

data

Template data store Object, use $.store(obj), abaaso.store(obj) or abaaso.data.register(obj) to create a data store on an existing Object. Data stores are evented, providing a rich structure for powerful applications. Views can be generated (and cached) by sorting the data store.

RESTful behavior is supported, if associated with a Collection, via the uri property.

Example

var twitter = {id: "twitter"},
    uri = "http://search.twitter.com/:api?callback=?:queries";

// Creating the store
$.store(twitter);
twitter.data.key      = "id";
twitter.data.callback = "callback";

// Listening for new records in the store
twitter.on("afterDataSet", function (rec) {
	// rec is an Object {key:, data:} 
});

// Making the store RESTful
typeof twitter.data.setUri === "function" ? twitter.data.setUri(uri) : twitter.data.uri = uri;

data.batch

Method

Batch sets or deletes data in the store

@param  {String}  type Type of action to perform
@param  {Mixed}   data Array of keys or indices to delete, or Object containing multiple records to set
@param  {Boolean} sync [Optional] Syncs store with data, if true everything is erased
@return {Object} Data store

Events

beforeDataBatch  Fires before the batch is queued
afterDataBatch   Fires after the batch is queued
failedDataBatch  Fires when an exception occurs

Example

obj.data.batch("set", [{}, {}]);

data.callback

Property

Triggers JSONP consumption of the uri property; must be set prior to setting uri

Example

obj.data.callback = "callback";

data.clear

Method

Clears the data store, unsets the uri property

@param {Boolean} sync [Optional] Boolean to limit clearing to records & views
@return {Object} Data store

Events

beforeDataClear  Fires before the data is cleared
afterDataClear   Fires after the data is cleared

Example

obj.data.clear();

data.credentials

Property

Sets "withCredentials" of XHR Object when consuming uri property; is not supported by IE 8 or 9 on CORS consumption

Example

obj.data.credentials = true;

data.del

Method

Deletes a record based on key or index

@param  {Mixed}   record  Record key or index
@param  {Boolean} reindex Default is true, will re-index the data object after deletion
@param  {Boolean} sync    [Optional] True if called by data.sync
@return {Object} Data store

Events

beforeDataDelete  Fires before the record is deleted
afterDataDelete   Fires after the record is deleted
syncDataDelete    Fires when the local store is updated
failedDataDelete  Fires if the store is RESTful and the action is denied

Example

obj.data.del("someKey");

data.expires

Property

Explicit timeout of the data store; expires the uri property value

Example

obj.data.expires = 500;

data.find

Method

Finds needle in the haystack

@param  {Mixed} needle   String, Number or Pattern to test for
@param  {Mixed} haystack [Optional] The field(s) to search
@return {Array} Array of results

Example

obj.data.find("joe"); // All records containing "joe" as a value

data.form

Method

Generates a micro-format form from a record

If record is null, an empty form based on the first record is generated

The submit action is data.set() which triggers a POST or PUT from the data store.

@param  {Mixed}   record null, record, key or index
@param  {Object}  target Target HTML Element
@param  {Boolean} test   [Optional] Test form before setting values
@return {Object} Generated HTML form

Example

obj.data.form(null, $("#target"));

data.get

Method

Retrieves a record based on key or index

If the key is an integer, cast to a string before sending as an argument!

@param  {Mixed}  record Key, index or Array of pagination start & end
@param  {Number} end    [Optional] Ceiling for pagination
@return {Mixed} Individual record, or Array of records

Example

var rec = obj.data.get("someKey"); // rec = {key: "someKey", data: {…}}

data.key

Property

Key property of record representation when consuming an API; if the API supports RESTful behavior the key will be used for URI mapping of CUD operations

Example

obj.data.key = "id";

data.records

Property

Array of data record Objects, with indices mapped to keys (do not use directly)

data.reindex

Method

Reindexes the data store

@return {Object} Data store

Example

obj.data.reindex();

data.register

Method

Registers a data store on an Object

@param  {Object} obj  Object to register with
@param  {Mixed}  data [Optional] Data to set with this.batch
@return {Object} Object registered with

Events

beforeDataStore  Fires before registering the data store
afterDataStore   Fires after registering the data store

Example

$.store(obj);

data.set

Method

Creates or updates an existing record

If a POST is issued, and the data.key property is not set the first property of the response object will be used as the key

@param  {Mixed}   key  Integer or String to use as a Primary Key
@param  {Object}  data Key:Value pairs to set as field values
@param  {Boolean} sync [Optional] True if called by data.sync
@return {Object} The data store

Events

beforeDataSet  Fires before the record is set
afterDataSet   Fires after the record is set, the record is the argument for listeners
syncDataSet    Fires when the local store is updated
failedDataSet  Fires if the store is RESTful and the action is denied

Example

obj.data.batch("set", [{}, {}]);

data.sort

Method

Returns a view, or creates a view and returns it

@param  {String} query   SQL (style) order by
@param  {String} create  [Optional, default is true] Boolean determines whether to recreate a view if it exists
@return {Array} View of data

Example

var sortedData = obj.data.sort("field1 desc, field2");

data.source

Property

Property of API response Object which has the Array of URIs or Objects to set

Example

obj.data.source = "data"; // API response = {data: {result: […]}, error: null}

data.sync

Method

Syncs the data store with a URI representation

@param {Boolean} reindex [Optional] True will reindex the data store
@return {Object} Data store

Events

beforeDataSync  Fires before syncing the data store
afterDataSync   Fires after syncing the data store
failedDataSync  Fires when an exception occurs

Example

obj.data.sync();

data.total

Property

Total records in the data store; incremented or decremented via set (use instead of records.length)

Example

obj.data.callback = "callback";

data.views

Property

Cached views of the data store, generated by sort; can be used directly, and returned from sort if pre-existing

Views are destroyed when the data store changes

Example

var sortedFieldAsc = obj.data.views["fieldAsc"];

data.uri

Property

Triggers RESTful consumption of an API

Example

obj.data.uri = "http://api.service.com";