UNPKG

@davidrouyer/vue-instantsearch

Version:

👀 Lightning-fast Algolia search for Vue apps

66 lines (63 loc) • 2.54 kB
import { createSuitMixin } from '../mixins/suit'; import { version } from '../../package.json'; // rollup does pick only what needed from json import { _objectSpread } from './polyfills'; import Vue from 'vue'; export const createInstantSearchComponent = component => _objectSpread( { mixins: [createSuitMixin({ name: 'InstantSearch' })], provide() { return { $_ais_instantSearchInstance: this.instantSearchInstance, }; }, watch: { searchClient(searchClient) { this.instantSearchInstance.helper.setClient(searchClient).search(); }, indexName(indexName) { this.instantSearchInstance.helper.setIndex(indexName).search(); }, stalledSearchDelay(stalledSearchDelay) { // private InstantSearch.js API: this.instantSearchInstance._stalledSearchDelay = stalledSearchDelay; }, routing() { throw new Error( 'routing configuration can not be changed dynamically at this point.' + '\n\n' + 'Please open a new issue: https://github.com/algolia/vue-instantsearch/issues/new?template=feature.md' ); }, searchFunction(searchFunction) { // private InstantSearch.js API: this.instantSearchInstance._searchFunction = searchFunction; }, }, created() { const searchClient = this.instantSearchInstance.client; if (typeof searchClient.addAlgoliaAgent === 'function') { searchClient.addAlgoliaAgent(`Vue (${Vue.version})`); searchClient.addAlgoliaAgent(`Vue InstantSearch (${version})`); } }, mounted() { // from the documentation: https://vuejs.org/v2/api/#mounted // "Note that mounted does not guarantee that all child components have also been mounted. If you want to // wait until the entire view has been rendered, you can use vm.$nextTick inside of mounted" this.$nextTick(() => { if (!this.instantSearchInstance.started) { this.instantSearchInstance.start(); } }); }, beforeDestroy() { if (this.instantSearchInstance.started) { this.instantSearchInstance.dispose(); } // a hydrated instance will no longer be hydrated once disposed, and starts from scratch this.instantSearchInstance.hydrated = false; }, }, component );