Skip to content

Instantly share code, notes, and snippets.

@kevin-brown
Last active October 28, 2019 01:35
Show Gist options
  • Save kevin-brown/47b1ad7fd555f4643992e4d91e1ce70d to your computer and use it in GitHub Desktop.
Save kevin-brown/47b1ad7fd555f4643992e4d91e1ce70d to your computer and use it in GitHub Desktop.
'use strict'
// eslint-disable-next-line camelcase
function set_taxon_select (selector) {
if ($(selector).length > 0) {
var url = Spree.url(Spree.routes.taxons_api, {
ids: $(selector).val().join(','),
without_children: true,
token: Spree.api_key
});
return $.getJSON(url, null, function (data) {
var options = $.map(data.taxons, function (taxon) {
return new Option(taxon.id, taxon.pretty_name, true, true);
});
$(selector)
.empty() // Clear out any existing selections
.append('<option></option>') // Add the placeholder option
.append(options); // Add the selected options
$(selector).select2({
placeholder: Spree.translations.taxon_placeholder,
ajax: {
url: Spree.routes.taxons_api,
datatype: 'json',
data: function (term, page) {
return {
per_page: 50,
page: page,
without_children: true,
q: {
name_cont: term
},
token: Spree.api_key
}
},
processResults: function (data, params) {
var taxons = $.map(data.taxons, function (taxon) {
taxon.text = taxon.text || taxon.pretty_name;
return taxon;
});
return {
results: taxons,
pagination: {
more: (param.page < data.pages)
}
}
}
}
});
});
}
}
$(document).ready(function () {
set_taxon_select('#product_taxon_ids')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment