All Projects → bhoriuchi → Vue Deepset

bhoriuchi / Vue Deepset

Licence: mit
Deep set Vue.js objects

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Vue Deepset

Vue Qq
🎨 Vue family bucket with socket.io and express/koa2 , create a web version of mobile QQ, supporting real-time group chat, real-time private chat, special care, shielding chat, smart IP geographic location, real-time display temperature and other QQ core functions
Stars: ✭ 861 (+937.35%)
Mutual labels:  vuex, vue2
Vue axios spa
基于vue2+axios+vux+vue-router+vuex构建的单页微信端项目
Stars: ✭ 54 (-34.94%)
Mutual labels:  vuex, vue2
Vms
A Vue.js 2.0 Content Management System
Stars: ✭ 885 (+966.27%)
Mutual labels:  vuex, vue2
Vue Entity Adapter
Package to maintain entities in Vuex.
Stars: ✭ 20 (-75.9%)
Mutual labels:  vuex, vue2
Vue Cnode
基于vue2 + vue-router + vuet + ES6 + less + flex.css重写vue版cnode社区,使用webpack2打包
Stars: ✭ 1,134 (+1266.27%)
Mutual labels:  vuex, vue2
Mall Vue
基于Vue+Vuex+iView的电子商城网站
Stars: ✭ 929 (+1019.28%)
Mutual labels:  vuex, vue2
Vuex Flash
VueJs Flash Message Component within Vuex
Stars: ✭ 54 (-34.94%)
Mutual labels:  vuex, vue2
Vue Music Player
🎵Vue.js写一个音乐播放器+📖One(一个).A music player + One by Vue.js
Stars: ✭ 729 (+778.31%)
Mutual labels:  vuex, vue2
Vue Login
vue2 express register login
Stars: ✭ 61 (-26.51%)
Mutual labels:  vuex, vue2
Vue Music
使用vue2.0构建音乐播放器
Stars: ✭ 60 (-27.71%)
Mutual labels:  vuex, vue2
Easy Vue
Learn vueJS Easily 👻
Stars: ✭ 896 (+979.52%)
Mutual labels:  vuex, vue2
Vue Mall
微信公众号测试项目
Stars: ✭ 74 (-10.84%)
Mutual labels:  vuex, vue2
Vue Chat
👥Vue全家桶+Socket.io+Express/Koa2打造一个智能聊天室。
Stars: ✭ 887 (+968.67%)
Mutual labels:  vuex, vue2
Vue Music
基于vue2.0的网易云音乐播放器,api来自于NeteaseCloudMusicApi,v2.0为最新版本
Stars: ✭ 855 (+930.12%)
Mutual labels:  vuex, vue2
Vue2 Study
vue 的webpack配置,按需加载,element-ui,vuex
Stars: ✭ 16 (-80.72%)
Mutual labels:  vuex, vue2
Vuestic Admin
Free and Beautiful Vue 3 Admin Template
Stars: ✭ 8,398 (+10018.07%)
Mutual labels:  vuex, vue2
Cordova Template Framework7 Vue Webpack
Framework7 - Vue - Webpack Cordova Template with Webpack Dev Server and Hot Module Replacement
Stars: ✭ 630 (+659.04%)
Mutual labels:  vuex, vue2
Vuex I18n
Localization plugin for vue.js 2.0 using vuex as store
Stars: ✭ 657 (+691.57%)
Mutual labels:  vuex, vue2
Pretty Vendor
[零食商贩] - 基于vue全家桶 + koa2 + sequelize + mysql 搭建的移动商城应用
Stars: ✭ 57 (-31.33%)
Mutual labels:  vuex, vue2
Vue Admin
基于Vue2、element ui、vue-cli、vuex、vue-router、axios 、echarts后台管理系统demo. 权限管理,用户管理,菜单管理。无限级菜单,下拉树形选择框
Stars: ✭ 1,135 (+1267.47%)
Mutual labels:  vuex, vue2

vue-deepset

Deep set Vue.js objects using dynamic paths


Binding deeply nested data properties and vuex data to a form or component can be tricky. The following set of tools aims to simplify data bindings. Compatible with Vue 1.x, Vue 2.x, Vuex 1.x, and Vuex 2.x

Note vueModel and vuexModel use Proxy objects if supported by the browser and fallback to an object with generated fields based on the target object. Because of this, it is always best to pre-define the properties of an object when using older browsers.

Also note that models are flat and once built can set vue/vuex directly using model[path] = value where path is a lodash formatted path string or path array.

Examples

Full examples can be found in the tests folder on the project repo

Requirements

Dynamic paths

If you knew what every path you needed ahead of time you could (while tedious) create custom computed properties with getter and setter methods for each of those properties. But what if you have a dynamic and deeply nested property? This problem was actually what inspired the creation of this library. Using a Proxy, vue-deepset is able to dynamically create new, deep, reactive properties as well as return undefined for values that are not yet set.

Path Strings

The modeling methods use lodash.toPath format for path strings. Please ensure references use this format. You may also use path Arrays which can be easier to construct when using keys that include dots.

The following 2 path values are the same

const stringPath = 'a.b["c.d"].e[0]'
const arrayPath  = [ 'a', 'b', 'c.d', 'e', 0 ]

Keys with dots

Since dots prefix a nested path and are also valid characters for a key data that looks like the following can be tricky

const data = {
  'foo.bar': 'baz',
  foo: {
    bar: 'qux'
  }
}

So care should be taken when building the path string (or just use an array path) as the following will be true

'foo.bar' // qux
'["foo.bar"]' // baz
'foo["bar"]' // qux
'["foo"].bar' // qux

Binding v-model to deeply nested objects

Model objects returned by $deepModel, vueModel, and vuexModel are flat and use getter/setter methods to access and update deep properties on the target object. Because of this, when binding a deep model object using v-model bracket notation should be used and the property value should be a path string. Using not notation past a single level object should be avoided.

<input type="text" v-model="model.prop1">
<input type="text" v-model="model['prop1.subprop']">
<input type="text" v-model="model['path.to[&quot;deep nested&quot;]']">

Usage

  • Webpack import * as VueDeepSet from 'vue-deepset'
  • Browser <script src='./node_modules/vue-deepset/vue-deepset.min.js'></script>

As a Plugin

The easiest way to set up vue-deepset is as a Vue.js plugin. The plugin adds instance methods $deepModel, $vueSet, and $vuexSet. For vuex a mutation must be added (see vuex section).

When using ES6+ use import * as VueDeepSet from 'vue-deepset'

  • $deepModel ( obj:Object ) - for local vue objects
  • $deepModel ( path:String ) - for vuex state properties; path to base object
  • $vueSet (obj:Object, path:String, value:*)
  • $vuexSet (path:String, value:*)

Example - Browser (Vue 2.x)

html

<div id="app">
  <input type="text" v-model="model['input1']">
  <input type="text" v-model="model['path.to[&quot;deep nested&quot;]']">
</div>

js

Vue.use(VueDeepSet)

var app = new Vue({
  el: '#app',
  store,
  computed: {
    model () {
      return this.$deepModel(this.obj)
    }
  },
  data: {
    obj: {
      input1: 'Hello World!',
      path: {
        to: {
          'deep nested': 'Hello Vue!'
        }
      }
    }
  }
})

Vuex

Form binding to vuex using v-model requires registering the provided VUEX_DEEP_SET mutation. A convienience method extendMutation has been provided.

html

<div id="app">
  <input type="text" v-model="formData['message']">
</div>

js

var store = new Vuex.Store({
  state: {
    formData: {
      message: 'Hello Vuex!'
    }
  },
  mutations: VueDeepSet.extendMutation({
    // other mutations
  })
})

var app = new Vue({
  el: '#app',
  store,
  computed: {
    formData () {
      return this.$deepModel('formData')
    }
  }
})

or

var store = new Vuex.Store({
  state: {
    formData: {
      message: 'Hello Vuex!'
    }
  },
  mutations: {
    VUEX_DEEP_SET: VueDeepSet.VUEX_DEEP_SET,
    // other mutations
  }
})

var app = new Vue({
  el: '#app',
  store,
  computed: {
    formData () {
      return this.$deepModel('formData')
    }
  }
})

API

vueSet ( obj:Object, path:String, value:* )

Deep sets a path with a value on a local data object so that is is reactive

vuexSet ( path:String, value:* )

Deep sets a path with a value in a vuex store so that it is reactive

VUEX_DEEP_SET ( state:VuexState, args:Object )

Vuex mutation that should be registered when using vuexSet

extendMutation ( [mutations:Object] )

Adds the VUEX_DEEP_SET mutation to an optional hash of mutations and returns an updated hash of mutations

vueModel ( obj:Object, [options:Object] )

Creates an abstracted model with a set of flat properties that are generated from inspecting the objects properties so that deeply nested properties can be accessed as first level properties

options

  • useProxy=true {Boolean} - disable use of Proxy when false
vuexModel ( path:String, [options:Object] )

The equivalent of vueModel for vuex. Path should point to the base object

options

  • useProxy=true {Boolean} - disable use of Proxy when false
deepModel ( obj:Object, [options:Object] )

Equivalent to vueModel

options

  • useProxy=true {Boolean} - disable use of Proxy when false
deepModel ( path:String, [options:Object] )

Equivalent to vuexModel

options

  • useProxy=true {Boolean} - disable use of Proxy when false

Non-Plugin usage

Example - Browser
var store = new Vuex.Store({
  state: {
    formData: {
      message: 'Hello Vuex!'
    }
  },
  mutations: VueDeepSet.extendMutation()
})

var app = new Vue({
  el: '#app',
  store,
  computed: {
    vuexForm () {
      return this.vuexModel('formData')
    },
    localForm () {
      return this.vueModel(this.localForm)
    }
  },
  methods: {
    vueSet: VueDeepSet.vueSet,
    vuexSet: VueDeepSet.vuexSet,
    vueModel: VueDeepSet.vueModel,
    vuexModel: VueDeepSet.vuexModel
  },
  data: {
    localForm: {
      message: 'Hello Vue!'
    }
  }
})

Example - Webpack + ES6

import { vueSet } from 'vue-deepset'

export default {
  methods: {
    clearForm () {
      vueSet(this.localForm, 'message', '')
    }
  },
  data: {
    localForm: {
      message: 'Hello Vue!'
    }
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].