Skip to content

Commit 6da7d90

Browse files
authored
fix(page): frame getter for custom Frames (NativeScript#9195)
1 parent 87418cd commit 6da7d90

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

packages/core/ui/frame/frame-common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getAncestor } from '../core/view-base';
1010
import { Builder } from '../builder';
1111
import { sanitizeModuleName } from '../builder/module-name-sanitizer';
1212
import { profile } from '../../profiling';
13+
import { FRAME_SYMBOL } from './frame-helpers';
1314

1415
export { NavigationType } from './frame-interfaces';
1516
export type { AndroidActivityCallbacks, AndroidFragmentCallbacks, AndroidFrame, BackstackEntry, NavigationContext, NavigationEntry, NavigationTransition, TransitionState, ViewEntry, iOSFrame } from './frame-interfaces';
@@ -700,6 +701,9 @@ export class FrameBase extends CustomLayoutView {
700701
}
701702
}
702703

704+
// Mark as a Frame with an unique Symbol
705+
FrameBase.prototype[FRAME_SYMBOL] = true;
706+
703707
export function getFrameById(id: string): FrameBase {
704708
console.log('getFrameById() is deprecated. Use Frame.getFrameById() instead.');
705709

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Unique symbol to mark Frame instances.
3+
*
4+
* We use a symbol because in some cases importing the Frame to do
5+
* instanceof checks introduces circular references.
6+
*
7+
* Having the symbol in it's own file prevents any potential circular
8+
* references and allows checking if an object is a frame
9+
*/
10+
export const FRAME_SYMBOL = Symbol('FRAME_SYMBOL');
11+
12+
/**
13+
* Helper to determine if the passed object is a Frame
14+
*
15+
* @param object
16+
*/
17+
export function isFrame(object: any) {
18+
return object && object[FRAME_SYMBOL] === true;
19+
}

packages/core/ui/page/page-common.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Style } from '../styling/style';
77
import { Color } from '../../color';
88
import { EventData } from '../../data/observable';
99
import type { Frame } from '../frame';
10+
import { isFrame } from '../frame/frame-helpers';
1011
import { ActionBar } from '../action-bar';
1112
import { KeyframeAnimationInfo } from '../animation/keyframe-animation';
1213
import { profile } from '../../profiling';
@@ -92,9 +93,9 @@ export class PageBase extends ContentView {
9293
}
9394

9495
get frame(): Frame {
95-
const frame = this.parent;
96+
const parent = this.parent;
9697

97-
return frame && frame.constructor.name === 'Frame' ? (frame as Frame) : undefined;
98+
return isFrame(parent) ? (parent as Frame) : undefined;
9899
}
99100

100101
private createNavigatedData(eventName: string, isBackNavigation: boolean): NavigatedData {

0 commit comments

Comments
 (0)