0.3.1 • Published 10 years ago

micro-store v0.3.1

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

MicroStore

micro store in es6 for client.

Motivation

I need micro store for learning some apps.

Getting Started

$ npm install micro-store
import MicroStore from 'micro-store';

API

  • defaults
  • create
  • update
  • destroy
  • get
  • order
  • where
  • limit
  • dispatchChange
  • dispatchCustomEvent
  • addChangeListener
  • removeChangeListener
  • addCustomEventListener
  • removeCustomEventListener
  • register
  • setData
  • getData

Options

let options = {
  localStorage: ture, // bool(default: true)
};

new SomeStore(options);

Example

if you use event emitter, I prepared MicroEmitter for this. Recommnd: MicroEmitter

import MicroStore from 'micro-store';
import { EventEmitter } from 'events';

let AppDispatcher = new EventEmitter(); // singleton

class TodoStore extends MicroStore {
  constructor() {
    super();
    this.defaults = {
      text: '',
      completed: false,
    };
    this.register(AppDispatcher, {
      'TODO_CREATE': (payload) => {
        this.create(payload.entity);
      },
      'TODO_UPDATE': (payload) => {
        this.update(payload.id, payload.updates);
      },
      'TODO_DESTROY': (payload) => {
        this.destroy(payload.id);
      },
    });
  }
}
export default new TodoStore();

class TodoItemComponent {
  constructor() {
    this.todos = TodoStore.get();

    // when TodoStore updated, call this.
    TodoStore.addChangeListener(this.render());

    // when TodoStore emit custom event(ex: PASS_VALIDATION), call this.
    TodoStore.addCustomEventListener('PASS_VALIDATION', this.fetch());
  }
  componentDidMount() {
    const isCreatModalShowing = TodoStore.getData('isCreatModalShowing');
    if (isCreatModalShowing) this.someaction();
  }
  /* ... */
  onClick() {
    TodoStore.setData('isCreatModalShowing', true);

    // if you don't use actions or dispatcher
    TodoStore.create({ text: 'Hello MicroStore' });

    // if you use action or dispatcher
    AppDispatcher.emit('TODO_CREATE', { text: 'Hello MicroStore' });
  }
}
TodoStore.get(); // get all data
TodoStore.get(id); // get an item
TodoStore.order('text').get();
TodoStore.order('text', true).get(); // reverse
TodoStore.where({ completed: true }).get();
TodoStore.limit(3).get();
TodoStore.where({ completed: true }).order('text').limit(5).get();
0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.13

10 years ago

0.1.12

10 years ago

0.1.11

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago