From 52fc193d0cbf8d9208b1151b6a302001a44cb582 Mon Sep 17 00:00:00 2001 From: Rajat Vig Date: Tue, 1 Dec 2015 22:18:56 -0800 Subject: [PATCH] Transfer table should update allocatedIds on allocated change Transfer table does a one time allocation for allocatedIds and does not watch changes to allocated. As a result, when the allocated list is fetched at a later point, the allocatedIds are not updated resulting in a broken view. Partially-Implements: blueprint ng-flavors Closes-Bug: #1531334 Change-Id: I807c682b69081f90fc3d74675e8ca149bb855521 --- .../transfer-table.controller.js | 12 ++++++++++- .../transfer-table.controller.spec.js | 20 +++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/horizon/static/framework/widgets/transfer-table/transfer-table.controller.js b/horizon/static/framework/widgets/transfer-table/transfer-table.controller.js index 21337c166d..acc8b8a1cb 100644 --- a/horizon/static/framework/widgets/transfer-table/transfer-table.controller.js +++ b/horizon/static/framework/widgets/transfer-table/transfer-table.controller.js @@ -100,7 +100,17 @@ }; ctrl.allocatedIds = {}; - angular.forEach(ctrl.allocated.sourceItems, function(item) { + markAllocatedItems(); + + $scope.$watchCollection(getAllocated, markAllocatedItems); + } + + function getAllocated() { + return ctrl.allocated.sourceItems; + } + + function markAllocatedItems() { + angular.forEach(ctrl.allocated.sourceItems, function flag(item) { ctrl.allocatedIds[item.id] = true; }); } diff --git a/horizon/static/framework/widgets/transfer-table/transfer-table.controller.spec.js b/horizon/static/framework/widgets/transfer-table/transfer-table.controller.spec.js index a63e5bde03..17c0b40ab0 100644 --- a/horizon/static/framework/widgets/transfer-table/transfer-table.controller.spec.js +++ b/horizon/static/framework/widgets/transfer-table/transfer-table.controller.spec.js @@ -23,12 +23,10 @@ beforeEach(module('smart-table')); beforeEach(module('horizon.framework')); - var log, params; + var log, params, scope; beforeEach(module(function($provide) { - // we will mock scope and timeout in this test // because we aren't concern with rendering results - var scope = { $apply: angular.noop }; var timeout = function(fn) { fn(); }; // we will mock parse and attrs @@ -41,16 +39,16 @@ }; }; - $provide.value('$scope', scope); $provide.value('$timeout', timeout); $provide.value('$parse', parse); $provide.value('$attrs', attrs); $provide.value('$log', log); })); - beforeEach(inject(function($injector) { + beforeEach(inject(function($injector, _$rootScope_) { + scope = _$rootScope_.$new(); params = { - '$scope': $injector.get('$scope'), + '$scope': scope, '$timeout': $injector.get('$timeout'), '$parse': $injector.get('$parse'), '$attrs': $injector.get('$attrs'), @@ -118,6 +116,7 @@ it('should swap out allocated item if allocation limit is one', testLimitOne); it('should deallocate by moving item from allocated to available list', testDeallocate); it('should update allocated on reorder', testUpdateAllocated); + it('should update allocatedIds if allocated change', testAllocatedIds); it('should toggle the views correctly on request', testToggleView); ////////// @@ -186,6 +185,15 @@ expect(trCtrl.numAvailable()).toEqual(1); } + function testAllocatedIds() { + expect(trCtrl.allocatedIds).toEqual({}); + + trCtrl.allocated.sourceItems = [{id: 1}, {id: 2}]; + scope.$apply(); + + expect(trCtrl.allocatedIds).toEqual({1: true, 2: true}); + } + function testUpdateAllocated() { var orderedItems = [1,2,3,4]; trCtrl.updateAllocated(null, null, orderedItems);