Skip to content
bredele edited this page Feb 11, 2014 · 8 revisions
  1. Class Store
  2. Methods

Class Store

The Store component is the model layer of your application. It's basically a wrapper for your models and collections that contains your data and all the logic surrounding it such as formatters, access control, computed properties, reset, local storage and can be easily extended with its middleware engine.

var Store = require('maple/store');

var model = new Store({
  name: 'olivier'
});

var collection = new Store([{
  name: 'olivier'
}, {
  name: 'amy'
}
]);

Store is really simple and reduces the complexity of your application, see article.

Store inherits from [Emitter](https://github.com/leafs/maple/wiki/Emitter and allows you to listen changes in your data.

Methods

store(data)

new Store(data)

  • data: Object or Array

    Create a new store with the given `data.

var Store = require('store');
var users = new Store([{
  name : 'eric'
},{
  name : 'olivier'
}]);

set

store.set(name, data)

  • name: String or Number (array index)
  • data: Any

Set an attribute name with data object.

object store:

store.set('nickname','bredele');

array store:

store.set(0,{
  name : 'amy'
});

Emits changed event with name, value, previous value.
Emits changed name event with value, previous value.

get

store.get(name)

  • name: String or Number (array index)

Get an attribute name.

object store:

store.get('nickname');

array store:

store.get(0);

del

store.del(name)

  • name: String or Number (array index)

Delete a store attribute.

store.del('nickname');

Emits deleted event with name.
Emits deleted name event.

on

store.on(name, callback, [scope])

  • name: String or Number (array index)

  • callback: Function

  • scope: optional callback's scope (Function)

    Listen events on Store.

store.on('change', function(name, val, previous) {
  ...
});

compute

store.compute(name, callback)

  • name: String or Number (array index)
  • callback: Function

Compute store properties into a new property.

store.compute('id', function(){
  return this.nickname + this.firstname;
});

Compute listen for changes on the computed properties and update automatically the new property.

format

store.format(name, callback)

  • name: String or Number (array index)

  • callback: Function

    Format an attribute output in Store.

store.format('nickname', function(val) {
  return 'hello ' + val;
});

store.get('nickname'); //hello bredele

reset

store.reset(data)

  • name: Object or Array

    Reset store with data.

store.reset([]);

Emits changed and/or deleted events.

local

store.local(name)

  • name: String

    Synchronize store with local storage.

store.local('mystore'); //reset with localstorage
...
store.local('mystore', true); //save in localstorage

use

store.use(callback)

  • callback: Function

    Use middleware to extend store.

store.use(function(obj) {
  obj.save = function() {
    //send to server
  };
});
...
store.save();
Clone this wiki locally