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

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)

Create a new store with the given data (Object or Array).

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

.set(name, data)

Set an attribute name with data object.

object store:

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

array store:

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

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

.get(name)

Get an attribute name.

object store:

store.get('nickname');

array store:

store.get(0);

.del(name)

Delete a store attribute.

store.del('nickname');

Emits deleted event with name.
Emits deleted name event.

.on(name, fn)

Listen events on Store.

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

.compute(name, fn)

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(name, fn)

Format an attribute output in Store.

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

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

.reset(data)

Reset store with data (Object or Array).

store.reset([]);

Emits change and/or deleted events.

.local(name)

Synchronize store with local storage.

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

.use(fn)

Use middleware to extend store.

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

Clone this wiki locally