-
Notifications
You must be signed in to change notification settings - Fork 0
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;
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", [{…}, {…}]);
Triggers JSONP consumption of the uri property; must be set prior to setting uri
Example
obj.data.callback = "callback";
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();
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;
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");
Explicit timeout of the data store; expires the uri property value
Example
obj.data.expires = 500;
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
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"));
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: {…}}
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";
Array of data record Objects, with indices mapped to keys (do not use directly)
Reindexes the data store
@return {Object} Data store
Example
obj.data.reindex();
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);
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", [{…}, {…}]);
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");
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}
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();
Total records in the data store; incremented or decremented via set (use instead of records.length)
Example
obj.data.callback = "callback";
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"];
Triggers RESTful consumption of an API
Example
obj.data.uri = "http://api.service.com";