Setting dijit.Tree cookie for all pages

I use the same dijit.Tree view for multiple pages in our application, and I would like the cookie to be saved for the server name and not the folder name.
Now I have 3 pages and 3 cookies, each of which stores its own information about the state of the tree, which is annoying.

Is there any way to do this? The only thing I found in cookies in the API is that I can set cookieName and enable / disable cookies.

+4
source share
1 answer

Tree.js doesn't seem to allow you to set attributes for a cookie. So I just had to rewrite the _saveState() method for Tree :

 var treeControl = new dijit.Tree({ model: treeModel, showRoot: false, openOnClick: false, cookieName: "OrganizationUnitTreeState", _saveState: function(){ // summary: // Create and save a cookie with the currently expanded nodes identifiers // Overre the default saveState function, so we can set the cookie path if(!this.persist){ return; } var ary = []; for(var id in this._openedItemIds){ ary.push(id); } dojo.cookie(this.cookieName, ary.join(","), {expires:365, path:"/"}); }, /* Many more methods */ }); 

This is the last line of code that does the trick. dojo.cookie() accepts a list of key / value pairs that will be converted into cookie attributes, so if you want all other attributes to be set, you will.

+4
source

Source: https://habr.com/ru/post/1305007/


All Articles