-
Notifications
You must be signed in to change notification settings - Fork 8
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.
new Store(data)
-
data:
ObjectorArrayCreate a new store with the given `data.
var Store = require('store');
var users = new Store([{
name : 'eric'
},{
name : 'olivier'
}]);store.set(name, data)
-
name:
StringorNumber(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.
store.get(name)
-
name:
StringorNumber(array index)
Get an attribute name.
object store:
store.get('nickname');array store:
store.get(0);store.del(name)
-
name:
StringorNumber(array index)
Delete a store attribute.
store.del('nickname');Emits deleted event with name.
Emits deleted name event.
store.on(name, callback, [scope])
-
name:
StringorNumber(array index) -
callback:
Function -
scope: optional callback's scope (
Function)Listen events on Store.
store.on('change', function(name, val, previous) {
...
});store.compute(name, callback)
-
name:
StringorNumber(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.
store.format(name, callback)
-
name:
StringorNumber(array index) -
callback:
FunctionFormat an attribute output in Store.
store.format('nickname', function(val) {
return 'hello ' + val;
});
store.get('nickname'); //hello bredelestore.reset(data)
-
name:
ObjectorArrayReset store with
data.
store.reset([]);Emits changed and/or deleted events.
store.local(name)
-
name:
StringSynchronize store with local storage.
store.local('mystore'); //reset with localstorage
...
store.local('mystore', true); //save in localstoragestore.use(callback)
-
callback:
FunctionUse middleware to extend store.
store.use(function(obj) {
obj.save = function() {
//send to server
};
});
...
store.save();