PDA

View Full Version : Moving code to function causes problems



bjnelson62
8 Nov 2012, 3:11 PM
I started with the following code:



refreshUIAfterDeletion: function() {
var treeSel = this.getTreeNodeSelection(),
me = this;

if (treeSel !== null) {
this.getGridCmp().getStore().reload();

if (treeSel.isExpanded() || treeSel.hasChildNodes()) {
treeSel.collapse(
false,
function() {
treeSel.removeAll(false, true);
treeSel.set('loaded', false); // mark as unloaded, ensure plus sign shown
},
me);
}
}
}
},



This all works fine.

But I needed that code to collapse and remove the children in a separate function, so I changed it to be:



collapseTreeNodeRemoveChildren: function(node) {
var me = this;

node.collapse(
false,
function() {
node.removeAll(false, true);
node.set('loaded', false); // mark as unloaded, ensure plus sign shown
},
me
);
},

refreshUIAfterDeletion: function() {
var treeSel = this.getTreeNodeSelection(),
me = this;

if (treeSel !== null) {
this.getGridCmp().getStore().reload();

if (treeSel.isExpanded() || treeSel.hasChildNodes()) {
this.collapseTreeNodeRemoveChildren(treeSel);
}
}
}
},



The tree gets updated fine, but now the grid refresh doesn't work. That seems really weird to me. Can anyone explain this, and even better, suggest a solution?

BTW, in order to not have the removeAll() call to not POST to the server (once for each child), I had to change removeAll(true) to be removeAll(false).

This means the nodes aren't being destroyed. Is there any downside to this? Is this a memory leak? Is there another/better way to avoid the POSTs to the server?

Thank you,


Brian

mitchellsimoens
10 Nov 2012, 6:20 AM
The grid doesn't refresh now? That call wasn't changed.

bjnelson62
13 Nov 2012, 6:00 AM
Yes, I know, hence my confusion and bewilderment.

I solved it with a hack. Now just before I collapse the node, I set a global variable to remember that. I then collapse the node, but don't supply a callback to delete the children. Instead, I created a handler for the collapsed event, and do everything there.

This is not ideal, but it's better than duplicated code, which is what I'd have to do if I didn't put that code into a separate function.