Skip to content

Instantly share code, notes, and snippets.

@bobylito
Created July 3, 2017 11:27
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 bobylito/0a9150db5bc51d0a277fac6fbec94c2e to your computer and use it in GitHub Desktop.
Save bobylito/0a9150db5bc51d0a277fac6fbec94c2e to your computer and use it in GitHub Desktop.
[instantsearch.js v2] Connector usage sample
// 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