This repository was archived by the owner on Mar 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstantsearch-best-result.js
More file actions
47 lines (46 loc) · 1.66 KB
/
instantsearch-best-result.js
File metadata and controls
47 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// instantsearch.js custom widget with plain JavaScript
// First, we add ourselves to the instantsearch.widgets namespace
instantsearch.widgets.bestResult = function bestResult(container) {
// See more details in our documentation:
// https://community.algolia.com/instantsearch.js/documentation/#custom-widgets
//
// You can use any JavaScript library you want.
//
// This is the custom widget interface (just an object). You need to implement
// at least render OR init.
return {
getConfiguration: function(/*currentSearchParams*/) {
// Here we only want one hit in the results, so we configure `hitsPerPage`.
//
// This parameter is one of Algolia's REST API: https://www.algolia.com/doc/rest#pagination-parameters
//
// See all the parameters here: https://www.algolia.com/doc/rest
//
// In the end, the underlying JS object being configured is the JavaScript helper of Algolia.
// See https://community.algolia.com/algoliasearch-helper-js/docs/SearchParameters.html
return {
hitsPerPage: 1
}
},
init: function(params) {
// params.state
// params.helper
//
// Nothing to do here, only present for documentation
},
render: function(params) {
// params.results
// params.state
// params.helper
var results = params.results;
if (results.hits.length === 0) {
container.innerHTML = 'No results';
} else {
container.innerHTML =
'Name: ' + results.hits[0].name + '<br/>' +
'Price: ' + results.hits[0].price + '<br/>' +
'Image: <img src="' + results.hits[0].image + '" />';
}
}
}
}