Giter VIP home page Giter VIP logo

vuex-models's Introduction

vuex-models

This package is aims to simplify v-model usage with your vuex state, by providing getter/action/mutation/state generator and mapper, that generates v-model compatible computed properties.

It is compatible with Vue 2.x

Installation

Just use npm:

npm i --save vuex-models

Usage

Using vuex-models is a dead simple - first you need to generate store fields like this:

// your imports
import { genVuexModels } from "vuex-models";

// Vue.use(Vuex), etc

/*
  First argument is an array of generated field names
  Second optional argument - state attribute name,
  where generated fields will be stored their states
*/
// models with initial state values

const models = genVuexModels(
  {
    foo: "defaultValueForFoo",
    bar: "defaultValueForBar",
  },
  "customField"
); // By default: Vxm

const store = new Vuex.Store({
  ...models,
});

/*
  Vuex state becomes to:

  {
    customField: {
      foo: 'defaultValueForFoo',
      bar: 'defaultValueForBar'
    }
  }
*/

export default store;

Then, in your vue components you can map computed properties by using mapVuexModels:

import { mapVuexModels } from "vuex-models";

export default {
  computed: {
    /*
      creating computed properties:
      foo: {
        get () { return this.$store.getters.autogeneratedGetterFoo },
        set (val) { this.$store.dispatch('autogeneratedAction', val) }
      }

      so, from now, you can safely use `foo` in v-model directives
    */
    ...mapVuexModels(["foo", "bar"]),
  },
};

Vuex namespaced stores

Also you can use vuex-models for namespaced vuex modules. All you need is to pass second argument to mapVuexModels with namespace name:

// for example we have store module namespaced with 'MyNamespacedModule'
/*
const store = new Vuex.Store({
  modules: {
    MyNamespacedModule: {
      namespaced: true,
      ...genVuexModels({
        foo: 'value'
      })
    }
  }
})
*/
export default {
  computed: {
    ...mapVuexModels(["foo"], "MyNamespacedModule"),
  },
};

Rename computed properties

Like vuex mappers, mapVuexModels supports properties renaming:

export default {
  computed: {
    // this.myFoo responds to store.foo model
    ...mapVuexModels({
      myFoo: "foo",
    }),
  },
};

vuex-models's People

Contributors

dependabot[bot] avatar ikenfin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vuex-models's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.