项目作者: InventiStudio

项目描述 :
Collection of functions that create mutations. DRY
高级语言: JavaScript
项目地址: git://github.com/InventiStudio/vuex-mutations.git
创建时间: 2017-10-09T17:05:02Z
项目社区:https://github.com/InventiStudio/vuex-mutations

开源协议:

下载


vuex-mutations, by InventiStudio

Factory of vuex mutations. DRY.

Table of Contents

Install

  1. # npm
  2. npm i --save @inventistudio/vuex-mutations
  3. # or yarn
  4. yarn add @inventistudio/vuex-mutations

Example

  1. import Mutations from '@inventistudio/vuex-mutations'
  2. const state = {
  3. // Prepare state
  4. me: {
  5. login: '',
  6. email: '',
  7. },
  8. comments: [],
  9. }
  10. const mutations = {
  11. // Magic
  12. SET_USER: Mutations.set('me'),
  13. UPDATE_USER_EMAIL: Mutations.set('me.email'),
  14. ADD_COMMENT: Mutations.add('comments'),
  15. UPDATE_COMMENT: Mutations.update('comments', { matchBy: 'comment_id' }),
  16. REMOVE_COMMENT: Mutations.remove('comments', { matchBy: 'comment_id' }),
  17. ADD_FULL_COMMENT: Mutations.addOrUpdate('comments', { matchBy: 'comment_id' })
  18. }
  19. const actions = {
  20. // Do your stuff here. Use mutations.
  21. SET_USER_ACTION({ commit }, user) {
  22. commit('SET_USER', user)
  23. }
  24. // (...)
  25. }

API

Mutations

Collection of functions that create mutations

Mutations.add(path, options) ⇒ Mutation(state, element)

It creates mutation that adds element to array

Kind: static method of Mutations

Param Type Default Description
path String Path to array
options Object
[options.position] String ‘first’ Determine if element should be inserted at the beginning (‘first’) or end (‘last’)

Example

  1. const mutations = {
  2. ADD_USER: add('users'),
  3. ADD_USER_AT_END: add('users', { position: 'last' }),
  4. }

Mutations.update(path, options) ⇒ Mutation(state, element)

It creates mutation that updates element of array. It does nothing if not found

Kind: static method of Mutations

Param Type Default Description
path String Path to array
options Object
[options.matchBy] function \ String (a, b) => a === b It can by function or propery name

Example

  1. const mutations = {
  2. UPDATE_BY_REFERENCE: update('path.to.array'),
  3. UPDATE_BY_ID: update('path.to.array', { matchBy: 'id' }),
  4. UPDATE_BY_CUSTOM_FUNC: update('path.to.array', { matchBy(a, b) { return a.name === b.name } }),
  5. }

Mutations.addOrUpdate(path, options) ⇒ Mutation(state, element)

It creates mutation that updates element of array if found. It adds it if not.

Kind: static method of Mutations

Param Type Default Description
path String Path to array
options Object
[options.position] String ‘first’ Determine if element should be inserted at the beginning (‘first’) or end (‘last’)
[options.matchBy] function \ String (a, b) => a === b It can by function or propery name

Example

  1. const mutations = {
  2. UPDATE_OR_ADD_BY_REFERENCE: addOrUpdate('path.to.array'),
  3. UPDATE_OR_ADD_BY_ID: addOrUpdate('path.to.array', { matchBy: 'data.id', position: 'last' }),
  4. UPDATE_OR_ADD_BY_CUSTOM_FUNC: addOrUpdate('path.to.array', { matchBy(a, b) { return a.name === b.name } }),
  5. }

Mutations.remove(path, options) ⇒ Mutation(state, element)

It creates mutation that removes element from array. It does nothing if not found

Kind: static method of Mutations

Param Type Default Description
path String Path to array
options Object
[options.matchBy] function \ String (a, b) => a === b It can by function or propery name

Example

  1. const mutations = {
  2. REMOVE_BY_REFERENCE: remove('path.to.array'),
  3. REMOVE_BY_ID: remove('path.to.array', { matchBy: 'id' }),
  4. REMOVE_BY_CUSTOM_FUNC: remove('path.to.array', { matchBy(a, b) { return a.name === b.name } }),
  5. }

Mutations.set(path) ⇒ Mutation(state, element)

It creates mutation that sets object property.

Kind: static method of Mutations

Param Type Description
path String Path to property

Example

  1. const mutations = {
  2. SET_SOMETHING: set('path.to.prop'),
  3. }