Created
July 3, 2017 11:27
-
-
Save bobylito/0a9150db5bc51d0a277fac6fbec94c2e to your computer and use it in GitHub Desktop.
[instantsearch.js v2] Connector usage sample
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
// custom `renderFn` to render the custom Menu widget | |
function renderFn(menuRenderingOptions, isFirstRendering) { | |
if (isFirstRendering) { | |
menuRenderingOptions.widgetParams.containerNode | |
.html('<select></select'); | |
menuRenderingOptions.widgetParams.containerNode | |
.find('select') | |
.on('change', function(event) { | |
menuRenderingOptions.refine(event.target.value); | |
}); | |
} | |
var options = menuRenderingOptions.items.map(function(item) { | |
return item.isRefined | |
? '<option value="' + item.value + '" selected>' + item.label + '</option>' | |
: '<option value="' + item.value + '">' + item.label + '</option>'; | |
}); | |
menuRenderingOptions.widgetParams.containerNode | |
.find('select') | |
.html(options); | |
} | |
// connect `renderFn` to Menu logic | |
var customMenu = instantsearch.connectors.connectMenu(renderFn); | |
// mount widget on the page | |
search.addWidget( | |
customMenu({ | |
containerNode: $('#custom-menu-container'), | |
attributeName: 'categories', | |
limit: 10, | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment