🚧 Site > Add Vue store

This commit is contained in:
brunosimon
2017-08-04 09:05:44 +02:00
parent 1aa3e5f135
commit eec6f8a796
15 changed files with 114 additions and 23 deletions
+17 -16
View File
@@ -1,32 +1,33 @@
import socketIoClient from 'socket.io-client'
import Connexion from '@/components/connexion'
import ProjectsHub from '@/components/projects-hub'
export default
{
name: 'application',
components:
{
Connexion,
ProjectsHub
},
data()
{
return {
counter: 0,
projects: { type: Object, value: {} }
counter: 0
}
},
computed:
{
projects()
{
return this.$store.state.projects
}
},
mounted()
{
const socketUrl = `${process.env.NODE_ENV === 'production' ? '' : 'http://localhost:1571'}/projects`
const socket = socketIoClient(socketUrl)
socket.on('connect', () =>
{
console.log('connected')
})
socket.on('update_projects', (data) =>
{
this.projects = data.all
})
window.setInterval(() =>
{
this.counter++
@@ -1,2 +0,0 @@
#application
background #f0f
@@ -1,6 +1,5 @@
<div id="application">
{{ counter }}
<div v-for="project in projects">
{{ project.name }}
</div>
<connexion></connexion>
<projects-hub></projects-hub>
</div>
+3
View File
@@ -0,0 +1,3 @@
<template src="./template.html"></template>
<script src="./script.js"></script>
<style src="./style.styl" lang="stylus"></style>
+22
View File
@@ -0,0 +1,22 @@
import socketIoClient from 'socket.io-client'
export default
{
name: 'connexion',
created()
{
this.url = `${process.env.NODE_ENV === 'production' ? '' : 'http://localhost:1571'}/projects`
this.socket = socketIoClient(this.url)
this.socket.on('connect', () =>
{
console.log('connected')
})
this.socket.on('update_projects', (data) =>
{
this.$store.commit('updateProjects', data.all)
})
}
}
@@ -0,0 +1,2 @@
<div>
</div>
@@ -0,0 +1,3 @@
<template src="./template.html"></template>
<script src="./script.js"></script>
<style src="./style.styl" lang="stylus"></style>
@@ -0,0 +1,17 @@
export default
{
name: 'projects-hub',
computed:
{
projects()
{
return this.$store.state.projects
}
},
created()
{
}
}
@@ -0,0 +1,5 @@
<div>
<a :href="project.slug" v-for="project in projects">
{{ project.name }}
</a>
</div>
+2
View File
@@ -2,12 +2,14 @@
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from '@/components/application/'
import store from '@/store.js'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
template: '<App/>',
components: { App }
})
+33
View File
@@ -0,0 +1,33 @@
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:
{
projects: [],
currentProject: null
},
mutations:
{
updateProjects(state, data)
{
for(const key in data)
{
const project = data[key]
state.projects.push(project)
}
},
addProject(state, data)
{
state.projects.push(data)
},
removeProject(state, data)
{
console.log(data)
}
}
})