/
taxons.js.coffee
51 lines (48 loc) 路 1.64 KB
/
taxons.js.coffee
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
48
49
50
$(document).ready ->
window.productTemplate = Handlebars.compile($('#product_template').text());
$('#taxon_products').sortable();
$('#taxon_products').on "sortstop", (event, ui) ->
$.ajax
url: Spree.routes.classifications_api,
method: 'PUT',
data:
product_id: ui.item.data('product-id'),
taxon_id: $('#taxon_id').val(),
position: ui.item.index()
if $('#taxon_id').length > 0
$('#taxon_id').select2
dropdownCssClass: "taxon_select_box",
placeholder: Spree.translations.find_a_taxon,
ajax:
url: Spree.routes.taxons_search,
datatype: 'json',
data: (term, page) ->
per_page: 50,
page: page,
q:
name_cont: term
results: (data, page) ->
more = page < data.pages;
results: data['taxons'],
more: more
formatResult: (taxon) ->
taxon.pretty_name;
formatSelection: (taxon) ->
taxon.pretty_name;
$('#taxon_id').on "change", (e) ->
el = $('#taxon_products')
$.ajax
url: Spree.routes.taxon_products_api,
data:
id: e.val
success: (data) ->
el.empty();
if data.products.length == 0
$('#sorting_explanation').hide()
$('#taxon_products').html("<h4>" + Spree.translations.no_results + "</h4>")
else
for product in data.products
if product.master.images[0] != undefined && product.master.images[0].small_url != undefined
product.image = product.master.images[0].small_url
el.append(productTemplate({ product: product }))
$('#sorting_explanation').show()