Sha256: a1a69b1c1ed253139e21015d3e99d5515c41c5551ee580725636766cbf564e0b

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# variant autocompletion
$(document).ready ->
  if $("#variant_autocomplete_template").length > 0
    window.variantTemplate = Handlebars.compile($("#variant_autocomplete_template").text())
    window.variantStockTemplate = Handlebars.compile($("#variant_autocomplete_stock_template").text())
    window.variantLineItemTemplate = Handlebars.compile($("#variant_line_items_autocomplete_stock_template").text())
  return

formatVariantResult = (variant) ->
  variant.image = variant.images[0].mini_url  if variant["images"][0] isnt `undefined` and variant["images"][0].mini_url isnt `undefined`
  variantTemplate variant: variant

$.fn.variantAutocomplete = (searchOptions = {}) ->
  @select2
    placeholder: Spree.translations.variant_placeholder
    minimumInputLength: 3
    initSelection: (element, callback) ->
      $.get Spree.routes.variants_search + "/" + element.val(), {}, (data) ->
        callback data
    ajax:
      url: Spree.routes.variants_api
      datatype: "json"
      quietMillis: 500
      params: { "headers": { "X-Spree-Token": Spree.api_key } }
      data: (term, page) =>
        searchData =
          q:
            product_name_or_sku_cont: term
          token: Spree.api_key
        _.extend(searchData, searchOptions)

      results: (data, page) ->
        window.variants = data["variants"]
        results: data["variants"]

    formatResult: formatVariantResult
    formatSelection: (variant) ->
      if !!variant.options_text
        variant.name + " (#{variant.options_text})"
      else
        variant.name

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_backend-1.0.0.pre app/assets/javascripts/spree/backend/variant_autocomplete.js.coffee.erb