Last active
August 29, 2015 14:05
-
-
Save nickcherry/a091f754f2f2cead34e1 to your computer and use it in GitHub Desktop.
Product Availability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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