代码之家  ›  专栏  ›  技术社区  ›  peirix

为所有页面设置dijit.tree cookie

  •  4
  • peirix  · 技术社区  · 15 年前

    我在应用程序的多个页面上使用相同的dijit.tree视图,我希望将cookie保存为服务器名,而不是文件夹名。
    现在我有3页和3个cookie,每个cookie都保存着自己关于树状态的信息,这有点烦人。

    有什么办法可以做到这一点吗?我在饼干上发现的唯一东西 API ,我可以设置 cookieName 打开/关闭饼干。

    1 回复  |  直到 15 年前
        1
  •  4
  •   peirix    15 年前

    似乎 Tree.js 不允许您设置cookie的属性。所以我不得不重写 _saveState() 方法 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 */
    });
    

    这是最后一行代码。这个 dojo.cookie() 获取键/值对的列表,这些键/值对将转换为cookie属性,因此如果您希望设置任何其他属性,请执行以下操作。