Skip to content

Commit dccddf6

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/flavor-snippets
2 parents 4e046cd + 22d0d38 commit dccddf6

27 files changed

+712
-9987
lines changed

.vitepress/config.mts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export default defineConfig({
2727
lang: 'en-US',
2828
title: 'NativeScript',
2929
description: 'NativeScript docs',
30+
head: [
31+
[
32+
'script',
33+
{
34+
async: 'true',
35+
src: 'https://vueschool.io/banner.js?affiliate=NSCRIPT&type=top',
36+
}
37+
],
38+
],
3039
ignoreDeadLinks: true,
3140
cleanUrls: true,
3241
lastUpdated: true,

content/configuration/nativescript.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ Discard any uncaught JS exceptions. This can be useful in production environment
359359

360360
### ios.SPMPackages
361361

362-
::: tip Experimental
362+
::: tip
363363

364-
SPM packages are available in `[email protected]` or newer and are experimental.
364+
SPM packages are available in `[email protected]` or newer.
365365

366366
:::
367367

content/core/observable.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ this.notifyPropertyChange('fruits', this.fruits)
7171

7272
### Avoiding Event Handler Memory Leak
7373

74-
To ensure that your app doesn't have memory leak caused by handlers that are no longer needed, use the [addWeakEventListener](#addweakeventlistener) function:
74+
To ensure that your app doesn't have memory leak caused by handlers that are no longer needed, use the [addWeakEventListener](#addweakeventlistener) function.
7575

7676
<!-- TODO: Add a working example -->
7777

@@ -144,21 +144,28 @@ Adds a one-time listener for the specified event.
144144

145145
### addWeakEventListener()
146146

147+
```ts
148+
observable.addWeakEventListener(source, eventName, callback, target)
149+
```
150+
151+
Attaches a Weak Event Listener.
152+
153+
- `source`: Observable class which emits the event.
154+
- `eventName`: The event name the specified listener listens to.
155+
- `callback`: The function which should be called when event occurs.
156+
- `target`: Subscriber (target) of the event listener. It will be used as a thisArg in the handler function.
157+
147158
### removeEventListener()
148159

149160
```ts
150-
Observable.removeEventListener(eventNames, callback, thisArg)
151-
152-
//or
153-
observable.removeEventListener(eventNames, callback, thisArg)
161+
observable.removeEventListener(eventNames, callback, target)
154162
```
155163

156164
Removes listener(s) for the specified event name(s).
157165

158-
- `eventNames` is a comma delimited string containing the names of the events the specified listener listens to.
159-
- _Optional_: The `callback` parameter points to a specific listener to be removed. If not defined, all listeners for the event names will be removed.
160-
<!-- Is the following definition correct -->
161-
- _Optional_: `thisArg` is a parameter used as `this` context in which the listener to be removed will be searched.
166+
- `eventNames`: Comma delimited string containing the names of the events the specified listener listens to.
167+
- `callback`: A specific listener to be removed. If not defined, all listeners for the event names will be removed.
168+
- `target`: Subscriber (target) of the event listener. It will be used as a thisArg in the handler function.
162169

163170
---
164171

content/guide/extending-classes-and-implementing-interfaces-android.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ This approach will not work if `application.android.ts` requires external module
282282

283283
@nativescript/core ships with a default `androidx.appcompat.app.AppCompatActivity` implementation, that bootstraps the NativeScript application, without forcing users to declare their custom Activity in every project. In some cases you may need to implement a custom Android Activity.
284284

285-
Create a new `activity.android.ts` or `activity.android.js` when using plain JS.
285+
Create a new `./src/activity.android.ts` or `./src/activity.android.js` when using plain JS.
286286

287287
::: info Note
288288
Note the `.android` suffix - we only want this file on Android.
@@ -493,9 +493,10 @@ To include the new Activity in the build, make sure it's added to the `webpack.c
493493
const webpack = require('@nativescript/webpack')
494494

495495
module.exports = (env) => {
496-
env.appComponents = (env.appComponents || []).concat(['./activity.android'])
497-
498496
webpack.init(env)
497+
env.appComponents = (env.appComponents || []).concat([
498+
'./src/activity.android',
499+
])
499500

500501
return webpack.resolveConfig()
501502
}

content/guide/navigation/modals.md

Lines changed: 125 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,143 @@ description: Navigation using modals - detached from the current backstack.
44
contributos:
55
- Ombuweb
66
- rigor789
7+
- flipperlite
78
---
89

910
## Showing a modal
1011

1112
To show a modal, call the [showModal](https://docs.nativescript.org/api/class/ViewCommon#showmodal) method on a [View](https://docs.nativescript.org/api/class/View) instance and pass it the path to the modal view file:
1213

14+
<!-- note: we need a "Open In StackBlitz" button, and also put these in an official NativeScript collection to link on the docs. -->
15+
<!-- A strongly typed, working, Typescript application can be [found here](https://stackblitz.com/edit/nativescript-stackblitz-templates-3bvo1g?file=app%2Fdetails-page.ts,app%2Fdetails-page.xml,app%2Fmain-page.ts). -->
16+
1317
```xml
14-
<Button text="View details" tap="onTap" />
18+
<Button text="Show a Modal" tap="onShowModal" />
1519
```
1620

1721
```ts
18-
onTap(args: EventData) {
19-
const button = args.object as Button
20-
button.showModal("details-page")
22+
import { EventData, View, ShowModalOptions } from '@nativescript/core'
23+
24+
export function onShowModal(args: EventData) {
25+
const view = args.object as View
26+
const options: ShowModalOptions = {
27+
// ...
28+
}
29+
view.showModal('details-page', options)
2130
}
2231
```
2332

33+
:::warning Note
34+
If your modal does not appear when tapping the button, confirm you set the correct path and the modal page exists. `showModal` does not throw an error when the provided path doesn't exist.
35+
:::
36+
2437
## Closing a modal
2538

26-
To close a modal, call the `closeModal` method of any `View` from the modal. Optionally pass it data you want to access in the parent page:
39+
To close a modal, call the `closeModal` method of any `View` from the modal.
40+
41+
For passing data back to the parent, see [Passing Data](#passing-data).
2742

2843
```xml
29-
<Button text="Close" tap="onTap" />
44+
<Button text="Close a Modal" tap="onCloseModal"/>
3045
```
3146

3247
```ts
33-
onTap(args: EventData) {
34-
const button = args.object as Button
35-
button.closeModal({
36-
name: 'John Doe - EDITED',
37-
})
48+
export function onCloseModal(args: EventData) {
49+
const view = args.object as View
50+
view.closeModal()
3851
}
3952
```
4053

41-
## Passing data to and from a modal
54+
## Passing data {#passing-data}
55+
56+
Modals are often used for prompting the user for input, this requires passing data between the parent and the modal.
4257

43-
Passing data can be done by passing a `context` in the `showModal` options parameter.
58+
### From parent to modal
59+
60+
To pass data to the modal, provide it in the `context` field of the ShowModalOptions:
4461

4562
```ts
63+
// main-page.ts
64+
import { ShowModalOptions } from '@nativescript/core'
65+
66+
// optionally use strong types
67+
import { ExampleModalContext } from './details-page'
68+
4669
const options: ShowModalOptions = {
47-
context: { name: 'John Doe' },
48-
closeCallback(args: { name: string }) {
49-
console.log('Modal closed', args.name)
50-
},
70+
context: {
71+
name: 'John Doe',
72+
} as ExampleModalContext,
5173
}
52-
// ...
74+
5375
button.showModal('details-page', options)
5476
```
5577

56-
To access the data in the modal, handle the `shownModally` event, and access it via `args.context`.
78+
In the modal page, listen to the `shownModally` event to access the `context`:
5779

5880
```xml
81+
<!-- details-page.xml -->
82+
<Page shownModally="onShownModally"/>
83+
```
84+
85+
```ts
86+
// details-page.ts
87+
import { ShownModallyData } from '@nativescript/core'
88+
89+
// optional strong type for the context
90+
export type ExampleModalContext = {
91+
name: string
92+
}
93+
94+
export function onShownModally(args: ShownModallyData) {
95+
const context = args.context as ExampleModalContext
96+
console.log(context.name) // 'John Doe'
97+
}
98+
```
99+
100+
### From modal to parent
101+
102+
When closing a modal, you can optionally pass data back to the parent. To do so, provide a `closeCallback` option in the ShowModalOptions:
103+
104+
```ts
105+
// main-page.ts
106+
107+
import { ShowModalOptions } from '@nativescript/core'
108+
109+
// optionally use strong types
110+
import { ExampleModalContext, ExampleModalResult } from './details-page'
111+
112+
const options: ShowModalOptions = {
113+
context: {
114+
name: 'John Doe',
115+
} as ExampleModalContext,
116+
closeCallback(result?: ExampleModalResult) {
117+
if (result) {
118+
console.log(`Modal returned: ${result.newName}`) // 'Modal returned: Jane Doe'
119+
return
120+
}
121+
console.log('Modal was cancelled.')
122+
},
123+
}
124+
125+
button.showModal('details-page', options)
126+
```
127+
128+
In the modal page, listen to the `shownModally` event to access the `context`.
129+
130+
::: code-group
131+
132+
```xml [details-page.xml]
59133
<Page shownModally="onShownModally">
60134
<StackLayout>
135+
<!-- ... -->
61136
<TextField text="{{ name }}" />
62-
<Button text="Close" tap="onClose"/>
137+
<Button text="Change Name" tap="onChangeName"></Button>
138+
<Button text="Cancel" tap="onCancel"></Button>
63139
</StackLayout>
64140
</Page>
65141
```
66142

67-
```ts
143+
```ts [details-page.ts]
68144
import {
69145
fromObject,
70146
Page,
@@ -73,17 +149,39 @@ import {
73149
EventData,
74150
} from '@nativescript/core'
75151

152+
// optional strong type for the context
153+
export type ExampleModalContext = {
154+
name: string
155+
}
156+
157+
export type ExampleModalResult = {
158+
newName: string
159+
}
160+
76161
export function onShownModally(args: ShownModallyData) {
77162
const page = args.object as Page
78-
const context = fromObject({
79-
...args.context,
80-
onClose(args: EventData) {
163+
const incomingContext = args.context as ExampleModalContext
164+
165+
const bindingContext = fromObject({
166+
...incomingContext,
167+
onChangeName(args: EventData) {
81168
const button = args.object as Button
82169
button.closeModal({
83-
name: context.name, // 'John Doe - EDITED'
84-
})
170+
newName: bindingContext.name, // 'Jane Doe'
171+
} as ExampleModalResult)
172+
},
173+
onCancel(args: EventData) {
174+
const view = args.object as View
175+
view.closeModal()
85176
},
86177
})
87-
page.bindingContext = context
178+
page.bindingContext = bindingContext
88179
}
89180
```
181+
182+
:::
183+
184+
## Additional Resources
185+
186+
- [NativeScript XML Data Binding](/guide/data-binding)
187+
- [TextField](/ui/text-field)

0 commit comments

Comments
 (0)