Last active
October 28, 2019 01:35
-
-
Save kevin-brown/47b1ad7fd555f4643992e4d91e1ce70d to your computer and use it in GitHub Desktop.
This file contains 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
'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