Skip to content

wejendorp/local-collection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

local-collection

The local-collection API exposes localStorage powered collections courtesy of nbubna/store, with the added power of component/enumerable.

Idea: Use localStorage to store and retrieve models, making sure each model (by id) only exists once in the application.

This will allow views using the same model to always stay in sync.

It is built to contain instances of component/model.

Installation

$ component install wejendorp/local-collection

Example

// user-collection
var LocalCollection = require('local-collection');

var model = require('model').attr('id').attr('name');
module.exports = new LocalCollection(model);
var UserCollection = require('user-collection');
// look up the key `user.me` and return a model even if it doesnt exist:
var me = UserCollection.obtain('me', {create: true});
me.name('Wejendorp');
me.store(); // Write it back to collection.

API

set(models)

Adds the model(s) to cache or updates an already existing model with the same id.

model.store()

The model is extended with a store method, equivalent to model.save, but only writing to localStorage.

obtain(id, create)

Returns the model with the chosen id from cache.

remove(id)

Removes the model from cache.

clear()

Clears the collection store

Enumerable

All component/enumerable methods are available for filtering and processing.

Dependencies

License

MIT