Giter Site home page Giter Site logo

vuex-rails-plugin's Introduction

VuexRailsPlugin

A Vuex plugin that easily maps Rails resources to Vuex modules

The idea of this plugin is to easily incorporate conventional rest endpoints defined when scaffolding Rails resources and map the responses to appropriate state in Vuex. ex. resources :posts generates the following endpoints:

  • GET|POST /posts
  • GET|PUT /posts/:id
  • DELETE /posts/:id

Install

$ npm i vuex-rails-plugin

Usage

The plugin can be imported into any Vuex store like so:

// store.js
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
import VuexRailsPlugin from 'vuex-rails-plugin/src/VuexRailsPlugin'

export default new Vuex.Store({
  // ...
  plugins: [
    VuexRailsPlugin('posts'),
    VuexRailsPlugin('categories')
  ]
})

This example defines Vuex modules for 'posts' and 'categories' namespaces. It supports the following state:

  • all - items returned from the index action
  • current - set when the show action is called
  • error - set if any errors occur such as validations

The state can be mapped to any Vue component using mapState

// some-component.vue
import { mapState } from 'vuex'
export default {
  computed: {
    ...mapState('posts', {
      posts: state => state.all,
      currentPost: state => state.current
      error: state => state.error
    })
  }
}

Actions can also be called by using mapActions

// some-component.vue
import { mapActions } from 'vuex'
export default {
  // ...
  methods: {
    ...mapActions('posts', {
      getPosts: 'getAll', // ex. this.getAll()
      getPost: 'get', // ex. this.getPost(id)
      createPost: 'create', // ex. this.createPost({title: 'Bad', body: 'Ass'})
      updatePost: 'update', // ex. this.updatePost({id: 1, title: 'Bad', body: 'Mama Jama'})
      destroyPost: 'destroy' // ex. this.destroyPost({})
    })
  }
}

Params are supported

this.getPosts({page: 1, limit: 10})

Use directly in a form

export default {
  data() {
    return {
      newPost: {
        title: '',
        body: ''
      }
    }
  }
}
<form @submit.prevent="createPost(newPost)">
  <label>Title</label>
  <input type="text" v-model="newPost.title"/>
  <label>Body</label>
  <input type="text" v-model="newPost.body">
  <buton type="submit">Submit</buton>
</form>

Promises supported

export default {
  // ...
  methods: {
    promiseMeThisPostWillCreate() {
      this.createPost(this.newPost)
        .then(createdPost => {
          alert('Thanks for keeping your promise!')
        })
        .catch(err => {
          alert('Why did you break my promise?')
        })
    }
  }
}

TODO

  • Support nested resources ex. /categories/:category_id/posts
  • Support other options that may be necessary to support custom getters, filters, etc.

vuex-rails-plugin's People

Contributors

rlafranchi avatar vrtsev avatar

Watchers

James Cloos avatar

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.