This short post will highlight the basics of using the Kendo UI for Vue components with the Vuex library.
The Vuex library is the suggested (by Vue) utility to implement the Flux pattern in your Vue app. It enables you to manage states and have a clean data layer, separated from the presentation layer of your app. If you haven't read the Vuex documentation, I strongly suggest you go through it and get back to this article.
The sample app that we are going to start creating here is available for you in this GitHub repository: kendo-with-vuex. You can clone the repo, play with it and have fun with Kendo UI, Vue and Vuex.
The Kendo UI for Vue components have built-in functionality to support the native Vue reactivity. That enables Vuex to automatically propagate changes to the Kendo UI components and update them along with data and state changes.
That means that any props that are passed to a Kendo UI component will propagate changes (when changed) and update the widget that the Kendo UI component implements.
Now, let's include Vuex in our basic Vue app. If you are wondering how to create a simple Vue app with Kendo UI, you can check out the article, Get Going with Kendo UI & Vue: A GIF Guide, or the Getting Started with Kendo UI and Vue Video Tutorial. If you are planning a Vue project, we recommend downloading the whitepaper, Planning a Vue Application.
Nothing too fancy here. Just follow the guide from the Vuex documentation:
npm install vuex --save
import Vuex from 'vuex'
Vue.use(Vuex)
import Vue from 'vue'
import Vuex from 'vuex'
import customers from '../assets/customers'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
customers: JSON.parse(JSON.stringify(customers))
}
})
import Vue from 'vue'
import App from './App.vue'
import store from './store'
import 'babel-polyfill'
import '@progress/kendo-ui'
import '@progress/kendo-theme-default/dist/all.css'
import { Grid, GridInstaller } from '@progress/kendo-grid-vue-wrapper'
Vue.use(GridInstaller)
new Vue({
el: '#app',
store,
render: h => h(App)
})
<template>
<div id="app">
<kendo-grid ref="grid" :data-source="customers" :editable="'inline'">
<kendo-grid-column field="ContactName" title="Contact Name">
<kendo-grid-column field="ContactTitle" title="Contact Title">
<kendo-grid-column field="CompanyName" title="Company Name">
<kendo-grid-column field="Country">
<kendo-grid-column :command="['edit', 'destroy']">
</kendo-grid>
</div>
</template>
<script>
export default {
name: 'app',
computed: {
customers () {
return new kendo.data.DataSource({
data: this.$store.state.customers
})
}
}
}
</script>
Running the app now will render a Kendo UI Grid bound to the customers data from the customers.json file. Up to now we used the Vuex library to have a separate layer that reads the data. Editing the items from the Grid, however, will not communicate with the state.
For simplicity, in this case, we will use mutations to communicate with the Vuex state.
getters: {
customerIds: state => {
return state.customers.map((item) => {
return item.CustomerID
})
}
},
mutations: {
edit (state, customer) {
var foundCustomer = state.customers.find((cust) => {
return cust.CustomerID === customer.CustomerID
})
var index = state.customers.indexOf(foundCustomer)
state.customers[index] = customer
},
remove (state, index) {
state.customers.splice(index, 1)
}
}
Using the Kendo UI Events enables you to control which exact mutation or action to use to dispatch a change. Plus, it gives you the power to programmatically interact with the widget and pass additional data to Vuex to accomplish more complex tasks. For our demo, using the save and remove events is enough.
<kendo-grid ref="grid" :data-source="customers" :editable="'inline'" @save="onSave" @remove="onRemove">
methods: {
onSave (ev) {
this.$store.commit('edit', ev.model.toJSON())
ev.sender.refresh()
},
onRemove (ev) {
this.$store.commit('remove', ev.row)
}
}
You might have noticed two particular things implemented in the save handled - the toJSON()
and refresh()
methods.
The toJSON()
method strips out any methods and properties injected by the Kendo UI DataSource, thus giving Vue an object with fewer fields to observe.
Calling refresh()
will close the editing UI for the case. This will not be needed if a service is used along with the DataSource. The refreshing is needed particularly when binding with local data.
If you run the app now you’ll see nothing new that Kendo UI Grid does not already do; listing, editing, and removing items. But behind the scenes all the editing is synced with Vuex and controlled using the Flux pattern.
But how do we see the results?
This tool not only enables you to inspect components, events, but also gives you all the information of what happens with Vuex. Install the plugin and test it out:
This is how you can be certain that the Vuex implementation works and that the Kendo UI components you are using in your app are integrating Vuex properly.
This was short and fairly easy to get done. You can check out a more advanced sample app in our GitHub repo, kendo-with-vuex, which uses a router, several Kendo UI components synced with Vuex and so on. Check it out, clone it, play with it, learn Kendo UI and have fun developing your app.
Share your thoughts about Vue, Vuex and Kendo UI in a comment (below). Let us know if you are using them successfully in your apps. Thank you for reading my article and sharing the coolness of Vue with me.
Yanko is a software developer in the Kendo UI team. He started as a support engineer for the ASP.NET AJAX and Kendo UI for jQuery products and later became a developer contributing to the JavaScript Kendo UI and .NET Telerik UI products. Besides his passion for web technologies, he loves being with his family and enjoys his photography hobby.