Hi, I am trying to set up a search page and am trying to use v-model in a custom component, however, watching the v-model property is not updating 'getItems'. I see the changes getting reflected in the vue development tools in the postsRequest.params but i'm unable to fetch the updated items and the posts computed property remains unchanged.
I'd appreciate if you could point me in the right direction.
<template>
<main class="site-content">
<div class="container">
<div class="advanced-search">
<input type="text"
name="searchTerm"
placeholder="Search title..."
tabindex="1"
v-model="searchTerm">
<select v-model="searchAuthor">
<option value="">All</option>
<option
v-for="author in authors"
:value="author.id"
:key="author.id">{{ author.name }}</option>
</select>
<select v-model="searchTopic">
<option value="">All</option>
<option
v-for="topic in topics"
:value="topic.id"
:key="topic.id">{{ topic.name }}</option>
</select>
</div>
<section v-if="posts">
<post-item
v-for="post in posts"
:key="post.id"
:post="post"
/>
</section>
</div>
</main>
</template>
<script>
import PostItem from '@/components/template-parts/PostItem'
export default {
name: 'Search',
components: {
PostItem,
},
data() {
return {
postsRequest: {
type: 'quote',
params: {
per_page: 5,
search: null,
authors: null,
tags: null
},
showLoading: true
},
authorsRequest: {
type: 'authors',
params: {
per_page: 100,
},
},
topicsRequest: {
type: 'tags',
params: {
per_page: 100,
},
},
searchTerm: '',
searchAuthor: '',
searchTopic: ''
}
},
watch: {
"searchAuthor": function() {
this.postsRequest.params.search = null
this.postsRequest.params.authors = this.searchAuthor
this.postsRequest.params.tags = null
this.getPosts()
},
"searchTopic": function() {
this.postsRequest.params.search = null
this.postsRequest.params.authors = null
this.postsRequest.params.tags = this.searchTopic
this.getPosts()
},
"searchTerm": function() {
this.postsRequest.params.search = this.searchTerm
this.postsRequest.params.authors = null
this.postsRequest.params.tags = null
this.getPosts()
}
},
computed: {
authors () {
return this.$store.getters.requestedItems(this.authorsRequest)
},
topics () {
return this.$store.getters.requestedItems(this.topicsRequest)
},
posts() {
return this.$store.getters.requestedItems(this.postsRequest)
}
},
methods: {
getAuthors() {
return this.$store.dispatch('getItems', this.authorsRequest)
},
getTopics() {
return this.$store.dispatch('getItems', this.topicsRequest)
},
getPosts() {
return this.$store.dispatch('getItems', this.postsRequest)
},
},
created() {
this.getAuthors()
this.getTopics()
this.getPosts()
},
}
</script>