From 635ccc444daa2bf8f5bbb9df52603ba8398b40cb Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 12 Nov 2013 12:01:16 +1100 Subject: [PATCH] Adds another childrenOf option to clearing tree cache to clear a cached branch, this is used to fix U4-3465 Media tree not updating when creating folders and can be used elsewhere where modifying trees outside of it's context. --- .../src/common/services/tree.service.js | 21 +++++++++++++ .../common/dialogs/mediapicker.controller.js | 8 ++++- .../unit/common/services/tree-service.spec.js | 30 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js b/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js index 7a310a5d3b..0ce43585d3 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js @@ -126,6 +126,27 @@ function treeService($q, treeResource, iconHelper, notificationsService, $rootSc treeCache = _.omit(treeCache, cacheKey); } } + else if (args.childrenOf) { + //if childrenOf is supplied a cacheKey must be supplied as well + if (!args.cacheKey) { + throw "args.cacheKey is required if args.childrenOf is supplied"; + } + //this will clear out all children for the parentId passed in to this parameter, we'll + // do this by recursing and specifying a filter + var self = this; + this.clearCache({ + cacheKey: args.cacheKey, + filter: function(cc) { + //get the new parent node from the tree cache + var parent = self.getDescendantNode(cc.root, args.childrenOf); + //clear it's children and set to not expanded + parent.children = null; + parent.expanded = false; + //return the cache to be saved + return cc; + } + }); + } else if (args.filter && angular.isFunction(args.filter)) { //if a filter is supplied a cacheKey must be supplied as well if (!args.cacheKey) { diff --git a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/mediapicker.controller.js b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/mediapicker.controller.js index 29270f7fcf..ba834e5e9e 100644 --- a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/mediapicker.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/mediapicker.controller.js @@ -1,7 +1,7 @@ //used for the media picker dialog angular.module("umbraco") .controller("Umbraco.Dialogs.MediaPickerController", - function($scope, mediaResource, umbRequestHelper, entityResource, $log, imageHelper, eventsService) { + function($scope, mediaResource, umbRequestHelper, entityResource, $log, imageHelper, eventsService, treeService) { var dialogOptions = $scope.$parent.dialogOptions; $scope.options = { @@ -21,6 +21,12 @@ angular.module("umbraco") .addFolder($scope.newFolderName, $scope.options.formData.currentFolder) .then(function(data) { + //we've added a new folder so lets clear the tree cache for that specific item + treeService.clearCache({ + cacheKey: "__media", //this is the main media tree cache key + childrenOf: data.parentId //clear the children of the parent + }); + $scope.gotoFolder(data.id); }); } diff --git a/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js b/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js index 499d8c1d6b..8659eadf07 100644 --- a/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js +++ b/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js @@ -262,6 +262,36 @@ describe('tree service tests', function () { expect(cache.__content.root.children.length).toBe(3); }); + + it('removes cache children for a parent id specified', function () { + + var cache; + + treeService.getTree({ section: "content", cacheKey: "_" }).then(function (dd) { + treeService.loadNodeChildren({ node: dd.root.children[0] }).then(function () { + cache = treeService._getTreeCache(); + }); + }); + + $rootScope.$digest(); + $httpBackend.flush(); + + expect(cache.__content.root.children.length).toBe(4); + expect(cache.__content.root.children[0].children.length).toBe(4); + + treeService.clearCache({ + cacheKey: "__content", + childrenOf: "1234" + }); + + cache = treeService._getTreeCache(); + + console.log(" blah: " + cache.__content.root.children.length); + + expect(cache.__content.root.children.length).toBe(4); + expect(cache.__content.root.children[0].children).toBeNull(); + expect(cache.__content.root.children[0].expanded).toBe(false); + }); });