Skip to content

Instantly share code, notes, and snippets.

@willrjmarshall
Created April 8, 2014 07:19
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 willrjmarshall/10099138 to your computer and use it in GitHub Desktop.
Save willrjmarshall/10099138 to your computer and use it in GitHub Desktop.
Darkswarm.controller "CheckoutCtrl", ($scope, Order, storage) ->
window.tmp = $scope
$scope.order = $scope.Order = Order
storage.bind $scope, "user", { defaultValue: true}
$scope.disable = ->
$scope.user = false
$scope.details = true
storage.bind $scope, "details"
storage.bind $scope, "billing"
storage.bind $scope, "shipping"
storage.bind $scope, "payment"
# Validation utilities to keep things DRY
$scope.dirtyValid = (name)->
$scope.dirty(name) and $scope.valid(name)
$scope.dirty = (name)->
$scope.checkout[name].$dirty
$scope.valid = (name)->
$scope.checkout[name].$invalid
$scope.error = (name)->
$scope.checkout[name].$error
$scope.required = (name)->
$scope.error(name).required
$scope.email = (name)->
$scope.error(name).email
$scope.number = (name)->
$scope.error(name).number
Darkswarm.controller "DetailsSubCtrl", ($scope) ->
$scope.detailsValid = ->
$scope.detailsFields().every (field)->
$scope.checkout[field].$valid
$scope.detailsFields = ->
["order[email]",
"order[bill_address_attributes][phone]",
"order[bill_address_attributes][firstname]",
"order[bill_address_attributes][lastname]"]
$scope.emailValid = ->
$scope.dirtyValid('order[email]')
$scope.emailError = ->
return "can't be blank" if $scope.required('order[email]')
return "must be valid" if $scope.email('order[email]')
$scope.phoneName = "order[bill_address_attributes][phone]"
$scope.phoneValid = ->
$scope.dirtyValid($scope.phoneName)
$scope.phoneError = ->
return "can't be blank" if $scope.required($scope.phoneName)
return "must be a number" if $scope.number($scope.phoneName)
$scope.purchase = (event)->
event.preventDefault()
checkout.submit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment