From d508e7772ce05d216a218fda4b404c6f0c80a5b5 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 8 Jan 2020 16:14:29 +0100 Subject: [PATCH] Fix angular templates bug Angular controller data was being partially preserved when switching back and forth between tab templates, causing the ProductsCtrl to hold duplicate datasets when it is re-initialized after going from the shop tab to another tab, then back again. --- .../darkswarm/controllers/products_controller.js.coffee | 8 ++++++++ app/views/shop/products/_form.html.haml | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee index 684109f7e4..b7bff59def 100644 --- a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee @@ -80,3 +80,11 @@ Darkswarm.controller "ProductsCtrl", ($scope, $filter, $rootScope, Products, Ord $scope.query = "" $scope.taxonSelectors.clearAll() $scope.propertySelectors.clearAll() + + $scope.refreshStaleData = -> + # If the products template has already been loaded but the controller is being initialized + # again after the template has switched, refresh loaded data to avoid conflicts + if $scope.Products.products.length > 0 + $scope.Products.products = [] + $scope.update_filters() + $scope.loadProducts() diff --git a/app/views/shop/products/_form.html.haml b/app/views/shop/products/_form.html.haml index 7378ef2fea..5e45c44fa1 100644 --- a/app/views/shop/products/_form.html.haml +++ b/app/views/shop/products/_form.html.haml @@ -1,5 +1,5 @@ .footer-pad.small-12.columns - %products{"ng-controller" => "ProductsCtrl", "ng-show" => "order_cycle.order_cycle_id != null", "ng-cloak" => true } + %products{"ng-controller" => "ProductsCtrl", "ng-init" => "refreshStaleData()", "ng-show" => "order_cycle.order_cycle_id != null", "ng-cloak" => true } // TODO: Needs an ng-show to slide content down .row.animate-slide{ "ng-show" => "query || appliedPropertiesList() || appliedTaxonsList()" }