From b293efd110325f52bed3f1ab49d3946af3ee4623 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 8 Apr 2014 18:39:50 +1000 Subject: [PATCH] Fixing issue at https://github.com/angular/angular.js/wiki/Understanding-Scopes - hiding our attributes inside objects so child scopes can access them safely --- .../controllers/checkout_controller.js.coffee | 21 +++++++++++++++---- .../shop/checkout/_authentication.html.haml | 2 +- app/views/shop/checkout/_billing.html.haml | 2 +- app/views/shop/checkout/_details.html.haml | 4 +++- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee index 6ebd9d1367..93273b7ed0 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee @@ -1,16 +1,18 @@ Darkswarm.controller "CheckoutCtrl", ($scope, Order, storage) -> window.tmp = $scope $scope.order = $scope.Order = Order + $scope.accordion = {} 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" + + storage.bind $scope, "accordion.details" + storage.bind $scope, "accordion.billing" + storage.bind $scope, "accordion.shipping" + storage.bind $scope, "accordion.payment" # Validation utilities to keep things DRY $scope.dirtyValid = (name)-> @@ -28,10 +30,21 @@ Darkswarm.controller "CheckoutCtrl", ($scope, Order, storage) -> $scope.number = (name)-> $scope.error(name).number + +# READ THIS FIRST +# https://github.com/angular/angular.js/wiki/Understanding-Scopes + Darkswarm.controller "DetailsSubCtrl", ($scope) -> $scope.detailsValid = -> $scope.detailsFields().every (field)-> $scope.checkout[field].$valid + + $scope.$watch -> + $scope.detailsValid() + , (valid)-> + if valid + $scope.accordion.details = false + $scope.accordion.billing = true $scope.detailsFields = -> ["order[email]", diff --git a/app/views/shop/checkout/_authentication.html.haml b/app/views/shop/checkout/_authentication.html.haml index ac87ce8e6f..133cd69a09 100644 --- a/app/views/shop/checkout/_authentication.html.haml +++ b/app/views/shop/checkout/_authentication.html.haml @@ -1,5 +1,5 @@ %fieldset - %accordion-group{heading: "User", "is-open" => "user"} + %accordion-group{heading: "User", "is-open" => "accordion.user"} .row .large-4.columns.text-center{"ng-controller" => "AuthenticationActionsCtrl"} %button{"ng-click" => "toggle('/login')"} Login diff --git a/app/views/shop/checkout/_billing.html.haml b/app/views/shop/checkout/_billing.html.haml index ccf1d49091..1bbbc3fd39 100644 --- a/app/views/shop/checkout/_billing.html.haml +++ b/app/views/shop/checkout/_billing.html.haml @@ -1,5 +1,5 @@ %fieldset#billing - %accordion-group + %accordion-group{"is-open" => "accordion.billing"} %accordion-heading .row .large-6.columns diff --git a/app/views/shop/checkout/_details.html.haml b/app/views/shop/checkout/_details.html.haml index cb7c189ad2..ad8c5acbeb 100644 --- a/app/views/shop/checkout/_details.html.haml +++ b/app/views/shop/checkout/_details.html.haml @@ -1,5 +1,5 @@ %fieldset#details - %accordion-group{"is-open" => "details"} + %accordion-group{"is-open" => "accordion.details"} %div{"ng-controller" => "DetailsSubCtrl"} {{ detailsValid() }} %accordion-heading @@ -12,6 +12,7 @@ .row .large-6.columns = f.email_field :email, required: "", "ng-model" => "order.email", + "ng-change" => "next()", "ng-class" => "{error: '!emailValid()'}" %small.error.medium.input-text{"ng-show" => "emailValid()"} {{ emailError() }} @@ -27,6 +28,7 @@ .row .large-6.columns = ba.text_field :firstname, "ng-model" => "order.bill_address.firstname" + .large-6.columns = ba.text_field :lastname, "ng-model" => "order.bill_address.lastname"