Skip to content

Instantly share code, notes, and snippets.

@adamrights
Forked from gr2m/store.dreamcode.js
Created November 26, 2013 20:59
Show Gist options
  • Select an option

  • Save adamrights/7666141 to your computer and use it in GitHub Desktop.

Select an option

Save adamrights/7666141 to your computer and use it in GitHub Desktop.
// add a new object
var type = 'note';
var attributes = {color: 'red'};
store.add(type, attributes)
.done(function (newObject) {});
.fail(function (error) {});
// update an existing object
var type = 'note';
var id = 'abc4567';
var update = {size: 2};
store.update(type, id, update)
.done(function (updatedObject) {});
// find one object
var type = 'note';
var id = 'abc4567';
store.find(type, id)
.done(function (object) {});
// Load all objects
store.findAll()
.done(function (objects) {});
// Load all objects from one type
var type = 'note';
store.findAll(type)
.done(function (objects) {});
// remove an existing object
var type = 'note';
var id = 'abc4567';
store.remove(type, id)
.done(function (removedObject) {});
// EVENTS
// listen to store events
store.on('add', function (newObject) {});
// new doc created
store.on('add', function (newObject) {});
// existing doc updated
store.on('update', function (updatedObject) {});
// doc removed
store.on('remove', function (removedObject) {});
// any of the events above
store.on('change', function (event, changedObject) {});
// all listeners can be filtered by type
store.on('add:note', function (newObject) {});
store.on('update:note', function (updatedObject) {});
store.on('remove:note', function (removedObject) {});
store.on('change:note', function (event, changedObject) {});
// ... and by type and id
store.on('update:note:uuid123', function (updatedObject) {});
store.on('remove:note:uuid123', function (removedObject) {});
store.on('change:note:uuid123', function (event, changedObject) {});
// ... react on changes coming from remote
store.on('add:note', function (newObject, options) {
if (options.remote) {
// do something wit
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment