Model

The model is the base of your documents. Saving and interacting with documents happens directly through the Model.

Initialization

Creating Model Objects starts with inheriting the base model.

import { Model } from 'mobx-pouchdb';

export default class ToDoModel extends Model {
    @observable title = '';
    @observable isCompleted = false;
}

Save

Saving Models doesn't happen automatically. This is done on purpose in order to allow the most control over your Models/ModelStores.

Model does expose a save method which is used to save edits. This will be touched on later. In the meantime it's best to override the save method as follows.

import { Model } from 'mobx-pouchdb';
import PouchDB from 'pouchdb';

const todoPouch = new PouchDB('todo');

export default class ToDoModel extends Model {
    @observable title = '';
    @observable isCompleted = false;

    save() {
        todoPouch.put(this.toJS())
    }
}

toJS

toJS is straighforward. It converts the current Model to a PouchDB friendly version. Overriding toJS can be done in order to remove properties that you don't want showing up in your document.

results matching ""

    No results matching ""