Store

Store is a little project I released few days ago which is basically a wrapper for both data models and collections. I won’t explain in this post what is Store and that’s why I invite you to read its API doc for more information.

Instead, I will share three things I like about it.

Collections

Some frameworks introduce the concept of collection as an ordered sets of data models. I believe a collection is an Array and nothing more complicated. Here’s an example with Backbone collections:

var Todo = Backbone.Model.extend({
  defaults: {
    title: '',
    completed: false
  }
});

var TodosCollection = Backbone.Collection.extend({
  model: Todo
});

var myTodo = new Todo({title:'Read the whole book', id: 2});

// pass array of models on collection instantiation
var todos = new TodosCollection([myTodo]);

and the same with store:

//see the array??
var todos = new Store([{
  title: 'Read the whole book',
  completed: false
}]);

Computed properties

A computed property is the result of the computation/transformation of one or serveral store properties. Here’s an example of computed property with Ember:

App.Person = Ember.Object.extend({
  // these will be supplied by `create`
  firstName: null,
  lastName: null,

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName');
  }.property('firstName', 'lastName')
});

and with store:

store.compute('fullName', function() {
  return this.firstName + ' ' + this.lastName;
});

In the example above, fullName will change everytime firstName and/or lastName change.

Did you notice? Store doesn’t ask you to specify the properties you want to compute. It’s smart enough to parse the function you passed and get the properties to listen.

It's extensible

Store is focused around the data. It won’t provide handlers to save and send data to a remote server like a model does. However, store can be extended as follows:

//use middleware
store.use(function(scope) {
  scope.save = function() {
    //do something
  };
});

This feature is quite handy, you can imagine having middlwares to store your data into indexdb, be synchronized with a remote db, add validations and way more. As I’m writing, there is already a couchdb version of store and someone doing an indexdb middleware.