Last active
July 27, 2017 16:10
-
-
Save codediodeio/65b03a10beb58149a5e1029937d0abe0 to your computer and use it in GitHub Desktop.
Custom InstantSearchJS template with Angular bindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div *ngFor="let hit of hits | async"> | |
{{hit | json}} | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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