Sha256: fbb083bae3a774800ea57e15a8b9937261631fe1a2e93a49d250c676134a33cf

Contents?: true

Size: 1.87 KB

Versions: 37

Compression:

Stored size: 1.87 KB

Contents

Spree.AddStockItemView = Backbone.View.extend
  initialize: ->
    @$countInput = @$("[name='count_on_hand']")
    @$locationSelect = @$("[name='stock_location_id']")
    @$backorderable = @$("[name='backorderable']")

  events:
    "click .submit": "onSubmit"

  validate: ->
    locationSelectContainer = @$locationSelect.siblings('.select2-container')
    locationSelectContainer.toggleClass('error', !@$locationSelect.val())
    @$countInput.toggleClass('error', !@$countInput.val())

    locationSelectContainer.hasClass('error') || @$countInput.hasClass('error')

  onSuccess: ->
    selectedStockLocationOption = @$locationSelect.find('option:selected')
    stockLocationName = selectedStockLocationOption.text().trim()
    selectedStockLocationOption.remove()

    editView = new Spree.EditStockItemView
      model: @model
      stockLocationName: stockLocationName
    editView.$el.insertBefore(@$el)

    @model = new Spree.StockItem
      variant_id: @model.get('variant_id')
      stock_location_id: @model.get('stock_location_id')

    if @$locationSelect.find('option').length is 1 # blank value
      @remove()
    else
      @$locationSelect.select2()
      @$countInput.val("")
      @$backorderable.prop("checked", false)

  onSubmit: (ev) ->
    ev.preventDefault()
    return if @validate()

    @model.set
      backorderable: @$backorderable.prop("checked")
      count_on_hand: @$countInput.val()
      stock_location_id: @$locationSelect.val()
    options =
      success: =>
        @onSuccess()
        show_flash("success", Spree.translations.created_successfully)
      error: (model, response, options) =>
        show_flash("error", response.responseText)
    @model.save(null, options)

$ ->
  $('.js-add-stock-item').each ->
    $el = $(this)
    model = new Spree.StockItem
      variant_id: $el.data('variant-id')
    new Spree.AddStockItemView
      el: $el
      model: model

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
solidus_backend-2.4.2 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.4.1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.3.1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.2.2 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.1.1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.0.3 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-1.4.2 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-1.3.2 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.4.0 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.4.0.rc1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.4.0.beta1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.3.0 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.3.0.rc3 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.3.0.rc2 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.3.0.rc1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.3.0.beta1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.0.2 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-1.4.1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.2.1 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee
solidus_backend-2.2.0 app/assets/javascripts/spree/backend/stock_management/index_add_forms.coffee