Skip to content

Instantly share code, notes, and snippets.

@codediodeio
Last active July 27, 2017 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codediodeio/65b03a10beb58149a5e1029937d0abe0 to your computer and use it in GitHub Desktop.
Save codediodeio/65b03a10beb58149a5e1029937d0abe0 to your computer and use it in GitHub Desktop.
Custom InstantSearchJS template with Angular bindings
<div *ngFor="let hit of hits | async">
{{hit | json}}
</div>
import { Component, OnInit } from '@angular/core';
import { environment } from '../../environments/environment';
import { AngularFireDatabase } from 'angularfire2/database';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import * as instantsearch from 'instantsearch.js'
@Component({
selector: 'search-ui',
templateUrl: './search-ui.component.html',
styleUrls: ['./search-ui.component.scss']
})
export class SearchUiComponent implements OnInit {
search: any;
hits = new BehaviorSubject(null);
constructor(private db: AngularFireDatabase) { }
ngOnInit() {
const options = environment.algolia;
this.search = instantsearch(options);
let customHits = instantsearch.connectors.connectHits(this.renderFn)
this.search.addWidget(
customHits({
subject: this.hits
})
);
// search box widget
this.search.addWidget(
instantsearch.widgets.searchBox({
container: '#search-box',
autofocus: false,
placeholder: 'Search for books',
poweredBy: true
})
);
this.search.start();
}
renderFn(HitsRenderingOptions) {
HitsRenderingOptions.widgetParams.subject.next(HitsRenderingOptions.hits)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment