Skip to content

Instantly share code, notes, and snippets.

@jquerygeek
Created October 26, 2017 14:22
Show Gist options
  • Save jquerygeek/8a07ef6e259d6a0fa5628d71c3730990 to your computer and use it in GitHub Desktop.
Save jquerygeek/8a07ef6e259d6a0fa5628d71c3730990 to your computer and use it in GitHub Desktop.
custom store
// my-store.js
import Vue from 'vue'
import axios from 'axios'
export const myStore = new Vue({
data: {
messages: [],
contacts: []
},
methods: {
addMessage(newMessage) {
this.messages.push(newMessage)
},
getMesseges() {
return this.messages
},
updateMessages(messeges) {
this.messages = messeges
},
getUnreadMesseges() {
return this.messages.filter(function (message) {
return !message.viewed
})
},
getContacts() {
return this.contacts
},
deleteContact(contact) {
this.contacts.splice(this.contacts.indexOf(contact), 1)
},
storeContacts() {
return axios.get(`http://jsonplaceholder.typicode.com/users`)
.then(response => {
this.contacts = response.data
})
.catch(e => {
console.log(e)
})
}
}
});
// main.js
import { myStore } from './my-store.js'
Vue.prototype.$myStore = myStore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment