Sha256: 0f045e1dbbcb7b62b0b9f8106d149a1ce1c7d53082f9ceee735726123c396e55

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

import { observable } from 'mobx';
import invariant from 'invariant';
import { extend, isArray, isObject } from 'lodash';
import { createCollection } from 'mobx-decorated-models';

import Sync from './sync';

function extendAry(modelClass, models = [], options = {}) {
    invariant(isArray(models), 'models must be an array');
    const ary = createCollection(extend({ model: modelClass }, options));

    if (models.length) {
        ary.replace(models);
    }

    ary.$map = observable.map({
        lastServerMessage: '',
        syncInProgress:    '',
        errors:            {},
    });
    ary.$map.keys().forEach((prop) => {
        Object.defineProperty(ary, prop, {
            get() { return this.$map.get(prop); },
            set(val) { return this.$map.set(prop, val); },
        });
    });
    Object.defineProperties(ary, {
        syncUrl: {
            get() { return modelClass.syncUrl; },
        },
        syncData: {
            set(rows) {
                this.replace(rows);
            },
        },
    });
    ary.fetch = function(fetchOptions = {}) {
        return Sync.forCollection(this, fetchOptions);
    };
    if (options.fetch) {
        ary.fetch(isObject(options.fetch) ? options.fetch : {});
    }
    return ary;
}

export default class ModelCollection {
    constructor(model) {
        this.$model = model;
        this.$collection = extendAry(model, [], {}, this);
        return this;
    }
    create(models = [], options = {}) {
        return extendAry(this.$model, models, options, this);
    }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hippo-fw-0.9.7 client/hippo/models/collection.js
hippo-fw-0.9.6 client/hippo/models/collection.js
hippo-fw-0.9.5 client/hippo/models/collection.js
hippo-fw-0.9.4 client/hippo/models/collection.js
hippo-fw-0.9.3 client/hippo/models/collection.js
hippo-fw-0.9.2 client/hippo/models/collection.js