npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-model-vuex

v1.2.0

Published

A custom Vue directive that mimics v-model to easily interact with Vuex stores

Downloads

6

Readme

Vue-Model-Vuex

A custom Vue directive that mimics v-model to easily interact with Vuex stores

About

As soon as you introduce a Vuex store into your project, you can no longer use v-model on form elements to bind state from the store.

Using v-model will throw errors when it tries to update state following user input because a Vuex store must be updated by mutations (and actions) only.

You can get around this by implementing the longhand version of v-model, like so.

<template>
  <input 
    class="input" 
    name="item" 
    type="text" 
    :value="item.value"
    @input="update"
  />
</template>

But now we lose the beauty and clarity of v-model! Enter v-model-vuex, which mimics v-model to allow us to bind the value and a method once again.

Install

Install the directive using the following command

npm i vue-model-vuex

Import

Add the directive into your project and register it with your Vue instance

import Vue from 'Vue'
import store from './store'

import VueModelVuex from 'vue-model-vuex'

Vue.directive(VueModelVuex)

new Vue({
  el: '#app',
  store,
  template: '<App />',
  components: { App }
})

Usage

Now you can use the directive on any element where you would have previously used v-model.

Using v-model-vuex requires a method name to be passed to the directive via a :modifier. This is how v-model-vuex knows which method to call when the event is triggered.

By default, v-model-vuex will attempt to bind the correct event trigger for the element (e.g, a change event handler would be bound to a select element). You can override this behaviour by passing the directive an .argument with the event name. See the example below.

API

  • v-model-vuex sets up the directive
  • v-model-vuex:method defines the method to be called when the data updates
  • v-model-vuex:method.handler defines the event handler which triggers the method call

Example

<template>
  <!-- just pass the method name -->
  <!-- this would default to @input -->
  <input 
    class="input" 
    name="item" 
    type="text" 
    v-model-vuex:update="item.value"
  />
  
  <!-- optional, specify an event handler -->
  <!-- this would be @keyup -->
  <input 
    class="input" 
    name="item" 
    type="text" 
    v-model-vuex:update.keyup="item.value"
  />
</template>

<script>
  export default {
    data () {
      return {} // local state
    },
    computed : {
      // reference to store
      item () {
        return this.$store.state.item
      }
      
      // or use mapGetters
      ...mapGetters([
        'item'
      ])
    },
    methods : {
      // dispatch to store
      update (event) {
        this.$store.dispatch('updateItem', event)
      }
      
      // or use mapActions
      ...mapActions([
        'updateItem'
      ]),
      update (event) {
        this.updateItem(event)
      }
    }
  }
</script>

Changelog

v1.2.0

  • add better support for checkboxes, set checked prop not value

v1.1.0

  • update default event handler to input, mimic @input

v1.0.0

  • initial release