Multiple cookies for different trees not working correctly

Good afternoon,

I’ve built multiple different trees that are dynamically selectable; each is built via this code:
function buildDocumentationTreeTwo( documentationTreeDataID )
{
var tUrl = ‘/phaseTwo/documentationSystem/documentationSystemUtil.php?action=buildXmlDataTree&documentationTreeDataID=’ + documentationTreeDataID;

var xmlData = null;

$.ajax( {type:‘POST’, url: tUrl, data: null, success: function( response ) {
xmlData = response;
return( null ); },
async: false } );

var cookieID = ‘ds_’ + documentationTreeDataID;

if( typeof tree === ‘undefined’ )
tree = new dhtmlXTreeObject( “documentationTree”, “100%”, “100%”, 0 );
else
tree.deleteChildItems( 0 );

function singleClickHandler( id )
{
var tUrl = ‘documentationSystemUtil.php?action=showAssociatedDocuments&componentID=’ + id;

  updateNamedDiv( 'interactiveArea', tUrl );

}

tree.setSkin(‘dhx_skyblue’); tree.setImagePath(“/phaseTwo/javascript/dhtmlx/dhtmlxTree/codebase/imgs/csh_bluefolders/”);
tree.enableDragAndDrop(false);
tree.loadXMLString( xmlData, function() { tree.loadOpenStates( cookieID ); } );
tree.attachEvent( “onOpenEnd”, function() { tree.saveOpenStates( cookieID, ‘expires=Sun, 31 Jan 9999 22:00:00 GMT’ ); } );
tree.setOnClickHandler(singleClickHandler);
}

I’d like cookies to store the state of each unique tree. It’s working if I select one tree (stored with a cookie id of ds_5), then open a node (or two, or three), and then reload the page - the cookie properly remembers what nodes are open and builds the tree appropriately. However, if I select a different tree (stored with a cookie id of ds_73), and then open nodes in it, the cookie named ds_5 is updated as well with the nodeIDs from tree id ds_73.

I can view the cookies directly (I’m using Chrome), and verify that each cookie contains the values from the last tree that was selected:

Name: treeOpenStatexds_5
Content: 1,75,81
Domain: dream.ftc.avagotech.net
Path: /phaseTwo/documentationSystem
Send for: Any kind of connection
Accessible to script: Yes
Created: Thursday, April 3, 2014 12:23:14 PM
Expires: Sunday, January 31, 9999 3:00:00 PM

Name: treeOpenStatexds_73
Content: 1,75,81
Domain: dream.ftc.avagotech.net
Path: /phaseTwo/documentationSystem
Send for: Any kind of connection
Accessible to script: Yes
Created: Thursday, April 3, 2014 12:23:14 PM
Expires: Sunday, January 31, 9999 3:00:00 PM

Name: treeOpenStatexds_2
Content: 1,75,81
Domain: dream.ftc.avagotech.net
Path: /phaseTwo/documentationSystem
Send for: Any kind of connection
Accessible to script: Yes
Created: Thursday, April 3, 2014 12:23:14 PM
Expires: Sunday, January 31, 9999 3:00:00 PM

Am I doing something wrong?

Thanks for the help,
Brian

Hi Brian,

If there are two trees and each of them uses own cookie (cookie names are different), there won’t be a problem.
But possibly you meant one tree with different data (data are reloaded)… You should know that an event handler is not removed when you reload tree data. You need to call detachEvent method to remove the handler.

For example if you set the following event handler:

var h = tree.attachEvent(“onOpenEnd”, function(){…});

you need to call the following to remove it:

tree.detachEvent(h);

Thank you Alexandra! That was the exact problem. Thanks for the help.

  • Brian