开发者

Setting dijit.Tree cookie for all pages

开发者 https://www.devze.com 2022-12-24 17:38 出处:网络
I\'m using the same dijit.Tree view over seve开发者_StackOverflowral pages in our application, and I\'d like to have the cookie saved for the server name, instead of the folder name.

I'm using the same dijit.Tree view over seve开发者_StackOverflowral pages in our application, and I'd like to have the cookie saved for the server name, instead of the folder name.

Right now I've got 3 pages and 3 cookies, which each hold their own information on the state of the Tree, which is kinda annoying.

Any ways to accomplish this? The only thing I've found on cookies in the API, is that I can set the cookieName and turn cookies on/off.


Seems that Tree.js won't let you set attributes for the cookie. So I just had to overwrite the _saveState() method for the 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 */
});

It's the last line of code there that does the trick. The dojo.cookie() takes a list of key/value pairs, which will be transformed into cookie attributes, so if you want any other attributes set, this is how you'd do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消