Skip to content

Instantly share code, notes, and snippets.

@nickcherry
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcherry/a091f754f2f2cead34e1 to your computer and use it in GitHub Desktop.
Save nickcherry/a091f754f2f2cead34e1 to your computer and use it in GitHub Desktop.
Product Availability
angular.module('JCP').factory 'Product', ($resource, Config) ->
$resource "#{ Config.BASE_API_URL }/products/:id", {},
getAvailability:
method: 'GET'
url: "#{ Config.BASE_API_URL }/products/:id/availability"
isArray: true
<div class="options" ng-repeat="option in product.options">
<div class="option">
<h5>Choose a {{ option.displayText | lowercase }}</h5>
<div class="option-value" ng-repeat="optionValue in option.optionValues">
<input type="radio"
ng-model="selectedOptions[option.displayText]"
ng-value="optionValue.value"
ng-disabled="optionValue.disabled">
<label>{{ optionValue.value }}</label>
</div>
</div>
</div>
angular.module('JCP').controller 'ProductController', ($scope, $stateParams, Product) ->
updateOptionAvailability = ->
if $scope.product?.options and $scope.availability
for option in $scope.product.options
for optionValue in option.optionValues
props = _.clone $scope.selectedOptions
props[option.displayText] = optionValue.value
optionValueAvailabilities = _.where $scope.availability, props
if optionValueAvailabilities.length is 0
optionValue.disabled = false
else if optionValueAvailabilities.length is 1
optionValue.disabled = !optionValueAvailabilities[0].available
else
optionValue.disabled = true
for optionValueAvailability in optionValueAvailabilities
if optionValueAvailability.available
optionValue.disabled = false
break
productId = $stateParams.productId
$scope.product = Product.get { id: productId }, updateOptionAvailability
$scope.availability = Product.getAvailability { id: productId }, updateOptionAvailability
$scope.selectedOptions = {}
$scope.$watch 'selectedOptions', updateOptionAvailability, true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment