0.0.2-alpha.11 • Published 6 years ago

vuex-model-template v0.0.2-alpha.11

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

vuex-model-template

Generation of vuex module from object template.

Alpha version. More information coming soon.

Objective

Working with Vuex, I spent a lot of time writing and perfecting boilerplate code, logic surrounding calls for actions/mutations and managing side effects.

This is my attempt at simplifying the boilerplate generation while adding support for:

  • enforcing expected properties
  • type validation
  • auto-generated actions accessible directly on the object's instance
  • automatic side-effects management
  • template based serialization & deserialization

Getting Started

Webpack Configuration

If you uglify your code, do not use the '-p' flag on the webpack executable. Also, make sure to use the mangle option shown below, otherwise it won't work as expected.

mangle: {
    keep_fnames: true /* Prevents function names (class names) from being compressed */
}

Setup Code

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

import VuexModelObject, { TYPE } from 'vuex-model-template';

const store = Vuex.Store({
    actions: {},
    mutations: {},
    modules: {}
});

VuexModelObject.use(store);

Usage

const FOO_MODEL = {
    template: {
        aAny: {
            type: TYPE.Any,
            defaultValue: undefined
        },
        aArray: {
            type: TYPE.Array,
            defaultValue: [],
            items: {
                type: TYPE.String,
            }
        },
        aBoolean: {
            type: TYPE.Boolean,
            defaultValue: false
        },
        aComplex: {
            type: TYPE.Complex,
            // defaultValue: {} <-- this property is not used for Complex
            properties: {
                aProp: {
                    type: TYPE.String,
                    // etc.
                }
            }
        },
        aNumber: {
            type: TYPE.Number,
            defaultValue: 10
        },
        aObject: {
            type: TYPE.Object,
            defaultValue: undefined
        },
        aString: {
        type: TYPE.String
        defaultValue: 'defined'
        },
        aSymbol: {
            type: TYPE.Symbol,
            defaultValue: undefined
        }
    },
    actions: {
        doAction(aNumber) {
            return this.actions.aNumber.set(aNumber);
        }
    },
    properties: {

    }
};

class FooClass extends VuexModelObject {
    constructor(data) {
        super(data, FOO_MODEL);
    }

    $initialize() {
        // $initialize will be executed before $waitReady returns the instance
        return this.actions.doAction(3);
    }
}

let foo = new FooClass();
foo.$waitReady.then(() => {
    console.log(foo.aNumber); // 10
    console.log(foo.aString); // 'defined'
    console.log(foo.invalidProp); // throws exception if dev || instrumented builds (wrapped in proxy)

    // Modify values through wrapped Vuex actions
    foo.actions.aNumber.set(20).then(res => {
        console.log(foo.aNumber); // 20
    });
    console.log(foo.aNumber); // 10

    // Call custom actions
    foo.actions.doAction(30).then(res => {
        console.log(foo.aNumber); // 30
    });

    // Actions op an array
    foo.actions.aArray.clear().then(() => {
        return foo.actions.aArray.add('new entry');
    }).then(() => {
        return foo.actions.aArray.remove('new entry');
    }).then(() => {
        return foo.actions.aArray.set(['123', '234', '345']);
    });
});

// Custom serialization and deserialization of properties
// For example: 'aChildObject' is a link to another object we wish to save as a string (id) and to expand as a full-fledged object whem running.
template['aChildObject'] = {
    type: TYPE.Object,
    serialize(item) {
        return item.id;
    },
    deserialize(id) {
        // This call can be synchronous or asynchronous
        return ChildObjectService.get(id);
    }
}

// Set 'aChildObject'
foo.actions.aChildObject.set({
    id: 'abc',
    prop1: 'prop1'
});

let json = JSON.parse(foo.toJSON());
console.log(json.aChildObject); // 'abc'

Usage Example

Go check the Example Project

0.0.2-alpha.12

6 years ago

0.0.2-alpha.11

6 years ago

0.0.2-alpha.10

6 years ago

0.0.2-alpha.9

7 years ago

0.0.2-alpha.8

7 years ago

0.0.2-alpha.7

7 years ago

0.0.2-alpha.6

7 years ago

0.0.2-alpha.5

7 years ago

0.0.2-alpha.4

7 years ago

0.0.2-alpha.3

7 years ago

0.0.2-alpha.2

7 years ago

0.0.1-alpha.47

7 years ago

0.0.1-alpha.45

7 years ago

0.0.1-alpha.44

7 years ago

0.0.1-alpha.42

7 years ago

0.0.1-alpha.41

7 years ago

0.0.1-alpha.40

7 years ago

0.0.1-alpha.39

7 years ago

0.0.1-alpha.38

7 years ago

0.0.1-alpha.37

7 years ago

0.0.1-alpha.36

7 years ago

0.0.1-alpha.35

7 years ago

0.0.1-alpha.34

7 years ago

0.0.1-alpha.33

7 years ago

0.0.1-alpha.32

7 years ago

0.0.1-alpha.31

7 years ago

0.0.1-alpha.30

7 years ago

0.0.1-alpha.29

7 years ago

0.0.1-alpha.28

7 years ago

0.0.1-alpha.27

7 years ago

0.0.1-alpha.26

7 years ago

0.0.1-alpha.25

7 years ago

0.0.1-alpha.24

7 years ago

0.0.1-alpha.23

7 years ago

0.0.1-alpha.22

7 years ago

0.0.1-alpha.21

7 years ago

0.0.1-alpha.20

7 years ago

0.0.1-alpha.19

7 years ago

0.0.1-alpha.18

7 years ago

0.0.1-alpha.17

7 years ago

0.0.1-alpha.16

7 years ago

0.0.1-alpha.14

7 years ago

0.0.1-alpha.13

7 years ago

0.0.1-alpha.12

7 years ago

0.0.1-alpha.11

7 years ago

0.0.1-alpha.10

7 years ago

0.0.1-alpha.9

7 years ago

0.0.1-alpha.8

7 years ago

0.0.1-alpha.7

7 years ago

0.0.1-alpha.6

7 years ago

0.0.1-alpha.5

7 years ago

0.0.1-alpha.4

7 years ago

0.0.1-alpha.3

7 years ago

0.0.1-alpha.2

7 years ago

0.0.1-alpha.1

7 years ago