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