Do not allow invalid quantity to reach model, triggering server update

This commit is contained in:
Rohan Mitchell
2016-04-29 11:56:48 +10:00
parent 65895752da
commit 88e9eb59cf
2 changed files with 18 additions and 8 deletions

View File

@@ -1,9 +1,19 @@
Darkswarm.directive "ofnOnHand", ->
restrict: 'A'
link: (scope, elem, attr) ->
on_hand = parseInt(attr.ofnOnHand)
elem.bind 'change', (e) ->
if parseInt(elem.val()) > on_hand
scope.$apply ->
alert t('insufficient_stock', {on_hand: on_hand})
elem.val(on_hand)
require: "ngModel"
link: (scope, elem, attr, ngModel) ->
# In cases where this field gets its value from the HTML element rather than the model,
# initialise the model with the HTML value.
if scope.$eval(attr.ngModel) == undefined
ngModel.$setViewValue elem.val()
ngModel.$parsers.push (viewValue) ->
on_hand = parseInt(attr.ofnOnHand)
if parseInt(viewValue) > on_hand
alert t('insufficient_stock', {on_hand: on_hand})
viewValue = on_hand
ngModel.$setViewValue viewValue
ngModel.$render()
viewValue

View File

@@ -30,7 +30,7 @@
-# "price-breakdown-placement" => "left",
-# "price-breakdown-animation" => true}
%td.text-center.cart-item-quantity{"data-hook" => "cart_item_quantity"}
= item_form.number_field :quantity, :min => 0, "ofn-on-hand" => variant.on_hand, :class => "line_item_quantity", :size => 5
= item_form.number_field :quantity, :min => 0, "ofn-on-hand" => variant.on_hand, "ng-model" => "line_item_#{line_item.id}", :class => "line_item_quantity", :size => 5
%td.cart-item-total.text-right{"data-hook" => "cart_item_total"}
= line_item.display_amount_with_adjustments.to_html unless line_item.quantity.nil?