Adding some SUPER clever magic and fixing some regression issues

This commit is contained in:
Will Marshall
2014-04-07 18:23:37 +10:00
parent 577c91aca5
commit fbcf06f5f5
7 changed files with 109 additions and 81 deletions

View File

@@ -1,32 +1,10 @@
Darkswarm.controller "CheckoutCtrl", ($scope, $rootScope, order) ->
Darkswarm.controller "CheckoutCtrl", ($scope, $rootScope, Order) ->
$scope.require_ship_address = false
$scope.order = order
$scope.initialize = ->
# Our shipping_methods comes through as a hash like so: {id: requires_shipping_address}
# Here we default to the first shipping method if none is selected
$scope.order.shipping_method_id ||= Object.keys(order.shipping_methods)[0]
$scope.order.ship_address_same_as_billing = true if $scope.order.ship_address_same_as_billing == null
$scope.shippingMethodChanged()
$scope.shippingPrice = ->
$scope.shippingMethod().price
$scope.cartTotal = ->
$scope.shippingPrice() + $scope.order.display_total
$scope.shippingMethod = ->
$scope.order.shipping_methods[$scope.order.shipping_method_id]
$scope.paymentMethod = ->
$scope.order.payment_methods[$scope.order.payment_method_id]
$scope.order = $scope.Order = Order
$scope.shippingMethodChanged = ->
$scope.require_ship_address = $scope.shippingMethod().require_ship_address if $scope.shippingMethod()
Order.shippingMethodChanged()
$scope.purchase = (event)->
event.preventDefault()
checkout.submit()
$scope.initialize()

View File

@@ -0,0 +1,32 @@
Darkswarm.factory 'Order', ($resource, Product, order)->
## I am being clever here
## order is a JSON object generated in shop/checkout/order.rabl
## We're extending this to add methods while retaining the data!
new class Order
constructor: ->
@[name] = method for name, method of order # Clone all data from the order JSON object
# Our shipping_methods comes through as a hash like so: {id: requires_shipping_address}
# Here we default to the first shipping method if none is selected
@shipping_method_id ||= Object.keys(@shipping_methods)[0]
@ship_address_same_as_billing = true if @ship_address_same_as_billing == null
@shippingMethodChanged()
shippingMethod: ->
@shipping_methods[@shipping_method_id]
shippingMethodChanged: =>
@require_ship_address = @shippingMethod().require_ship_address if @shippingMethod()
shippingPrice: ->
@shippingMethod().price
paymentMethod: ->
@payment_methods[@payment_method_id]
cartTotal: ->
@shippingPrice() + @display_total