Trying to create a simple shared data object for a simple CRUD app. Given the web app is fairly simple (just CRUD for approx 25 different forms/tables and 10 to 30 fields in each form) and our team is only 2 persons, we feel vuex maybe an overkill for this project.
Referring to SO QA here I have created another implementation a simple data store that uses a Vue object to store shared data. See blow code or codepen.
Both of us are server side developers and we are more comfortable with a class based repository pattern than a store/functional pattern. So we are hoping to use a similar pattern in vue for storing and retrieving data from api.
Does anyone have experience using a similar approach? Code below seems to work now! But would there be any performance issues? or any unexpected side effects?
console.clear()
var person = new Vue({
data: {
items: []
},
computed: {
message() {
return "Hello " + (this.items[0] ? this.items[0].name : 'name') + "!"
}
},
methods: {
updateName() {
this.items[0].name = "Jane Doe"
}
}
})
Vue.component("child-component-one", {
template: "<div>Name: {{ person.items[0] ? person.items[0].name : '' }}</div>",
data: () => ({
person: person
})
})
Vue.component("child-component-two", {
template: `<div> Name: <input v-if="person.items[0]" v-model=person.items[0].name /> </div>`,
data: () => ({
person: person
})
})
new Vue({
el: "#app2",
mounted() {
console.log(this.person.items)
},
data: {
person: person
},
methods: {
updateGlobalName: function () {
this.person.updateName()
alert(this.person.message)
},
loadData() {
new Promise((resolve) => setTimeout(resolve, 2 * 1000)).then(() => this.person.items = [{
name: "John Doe"
}])
}
},
created() {
console.log(this.person.items)
this.loadData()
}
})
<div id="app2">
Wait 2 seconds for data to load <br />
Message From Root:
{{ person.message }}
<button @click="updateGlobalName">Update Global Name</button>
<hr>
Child Component 1
<child-component-one></child-component-one>
<hr>
Child Component 2
<child-component-two></child-component-two>
</div>
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>