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.

This commit is contained in:
Shannon
2013-11-12 12:01:16 +11:00
parent 6dcfbaee7f
commit 635ccc444d
3 changed files with 58 additions and 1 deletions

View File

@@ -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) {

View File

@@ -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);
});
}

View File

@@ -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);
});
});