@@ -156,6 +156,13 @@ export class NodeStore {
156156 @action
157157 setContext ( obj ?: mendix . lib . MxObject ) : void {
158158 this . debug ( "Store: setContext" , obj ) ;
159+
160+ if ( this . contextObject === null || ! obj || ( this . contextObject && obj && this . contextObject . getGuid ( ) !== obj . getGuid ( ) ) ) {
161+ this . resetState = true ;
162+ this . calculateInitialParents = true ;
163+ this . rowObjects = [ ] ;
164+ }
165+
159166 if (
160167 this . contextObject !== null &&
161168 obj &&
@@ -212,14 +219,18 @@ export class NodeStore {
212219
213220 @action
214221 setRowObjects ( mxObjects : mendix . lib . MxObject [ ] , level ?: number , parent ?: string | null ) : void {
215- this . debug ( "store: setRowObjects" , mxObjects . length , level ) ;
216- const currentRows : RowObject [ ] = level === - 1 ? [ ] : [ ...this . rowObjects ] ;
222+ this . debug ( "store: setRowObjects" , mxObjects . length , level , this . resetState ) ;
223+ let currentRows : RowObject [ ] = level === - 1 ? [ ] : [ ...this . rowObjects ] ;
217224 let initialState : TableState = { context : this . contextObject ?. getGuid ( ) || null , expanded : [ ] , selected : [ ] } ;
218225
219226 const objectGuids = mxObjects . map ( obj => obj . getGuid ( ) ) ;
220227 const treeMapping : { [ key : string ] : string } = { } ;
221228 const rootObjectGuids : string [ ] = [ ] ;
222229
230+ if ( ! this . contextObject ) {
231+ currentRows = [ ] ;
232+ }
233+
223234 // Calculate parents when loading a whole tree;
224235
225236 if (
@@ -482,14 +493,21 @@ export class NodeStore {
482493 customID : "key"
483494 } ;
484495
496+ if ( ! this . contextObject ) {
497+ return [ ] ;
498+ }
499+
485500 try {
486501 const treeObjects = this . rowObjects . map ( r => r . treeObject ) ;
487502 const tree = arrayToTree ( treeObjects , arrayToTreeOpts ) ;
503+ const filteredTree = tree . filter ( treeEl => typeof treeEl . _parent === "undefined" && ! treeEl . _parent ) ;
488504
489505 // When creating the tree, it can be possible to get orphaned children (a node that has a parent id, but parent removed).
490506 // We filter these top level elements from the tree, as they are no longer relevant
491507
492- return tree . filter ( treeEl => typeof treeEl . _parent === "undefined" && ! treeEl . _parent ) ;
508+ this . debug ( "store: tree: " , filteredTree ) ;
509+
510+ return filteredTree ;
493511 } catch ( error ) {
494512 console . warn ( error ) ;
495513 return [ ] ;
0 commit comments