@@ -20,7 +20,9 @@ npm install @nativescript/ionic-portals
20
20
21
21
## Usage
22
22
23
- 1 . Register and create portals on app boot - [ Get a Portal API Key here] ( https://ionic.io/docs/portals/getting-started/guide ) :
23
+ ### 1. Register portals on app boot
24
+
25
+ [ Get a Portal API Key here] ( https://ionic.io/docs/portals/getting-started/guide ) :
24
26
25
27
``` ts
26
28
import { Application } from ' @nativescript/core'
@@ -29,21 +31,24 @@ import { IonicPortalManager } from '@nativescript/ionic-portals'
29
31
Application .on (Application .launchEvent , () => {
30
32
// Register IonicPortals
31
33
IonicPortalManager .register (' <portal-api-key>' )
32
-
33
- // Create as many Portals as you need to use in your app
34
- // By default, the app will look for folders equal to the portal id you use here
35
- // For iOS: App_Resources/iOS/webPortal
36
- // For Android: App_Resources/Android/src/main/asssets/webPortal
37
- IonicPortalManager .create (' webPortal' )
38
34
})
39
35
40
36
// boot app here, for example:
41
37
Application .run ({ moduleName: ' app-root' })
42
38
```
43
39
44
- 2 . Use in your views
40
+ Create as many Portals as you need to use in your app.
41
+
42
+ The app will look for folders within it's resources where the folder name is equal to the portal ` id ` you use to define each portal.
43
+
44
+ Given the following examples, ensure your web portal is built into the following folders:
45
+
46
+ - For iOS: ` App_Resources/iOS/webPortal `
47
+ - For Android: ` App_Resources/Android/src/main/asssets/webPortal `
45
48
46
- ### Vanilla/Plain/Core
49
+ ### 2. Use in your views
50
+
51
+ #### Vanilla/Plain/Core
47
52
48
53
``` xml
49
54
<Page xmlns =" http://schemas.nativescript.org/tns.xsd"
@@ -55,7 +60,7 @@ Application.run({ moduleName: 'app-root' })
55
60
</Page >
56
61
```
57
62
58
- ### Angular
63
+ #### Angular
59
64
60
65
``` ts
61
66
import { registerElement } from ' @nativescript/angular'
@@ -66,17 +71,74 @@ registerElement('IonicPortal', () => IonicPortal)
66
71
;< IonicPortal id = " webPortal" > </IonicPortal >
67
72
```
68
73
69
- ### API
74
+ ## Communication
75
+
76
+ - Send events from NativeScript to any web portal:
77
+
78
+ ```
79
+ IonicPortalManager.publishTopic('hello', { name: 'data from NativeScript' });
80
+ ```
81
+
82
+ - Subscribe to events sent from any web portal:
83
+
84
+ ```
85
+ const subscriptionId = IonicPortalManager.subscribeToTopic('useful-web-event', result => {
86
+ console.log('received web portal useful-web-event with data:', result.data);
87
+ });
88
+ ```
89
+
90
+ - Unsubscribe from events sent from any web portal:
70
91
71
- - ` IonicPortalManager.register(apiKey: string) ` : Register Portals when your app boots
92
+ ```
93
+ IonicPortalManager.unsubscribeFromTopic('useful-web-event', subscriptionId);
94
+ ```
95
+
96
+ ## API
72
97
73
- - https://ionic.io/docs/ portals/getting-started/guide#configure
98
+ Interact and configure portals via ` IonicPortalManager ` which provides the following APIs:
74
99
75
- - ` IonicPortalManager.create(portalId: string, startDir?: string) ` : Create a Portal
76
- - ` portalId ` : The portal id to register
77
- - ` startDir ` : Set the web applications directory. By default it will look for a folder named the same as the portalId as the location of the web assets. Use this optional 2nd arg if you would like the folder name to be different that the portalId.
100
+ ``` ts
101
+ class IonicPortalManager {
102
+ /**
103
+ * Register Portals when your app boots
104
+ * https://ionic.io/docs/portals/getting-started/guide#configure
105
+ * @param apiKey your portal api key
106
+ */
107
+ static register(apiKey : string ): void
108
+ /**
109
+ * Used to set the initial context of any portal id before the portal is shown
110
+ * @param id portal id
111
+ * @param initialContext data provided as the initial context to the portal
112
+ */
113
+ static setInitialContext(id : string , initialContext : any ): void
114
+ /**
115
+ * Define usage of non-official Capacitor Plugins via Android package names
116
+ * @param plugins list of non-official Capacitor package names
117
+ */
118
+ static setAndroidPlugins(plugins : Array <string >): void
119
+ /**
120
+ * Send a message to any web portal via publishing a topic (aka. event)
121
+ * @param topic name of topic/event
122
+ * @param data payload to send
123
+ */
124
+ static publishTopic(topic : string , data ? : any ): void
125
+ /**
126
+ * Listen to any message sent from any web portal via subscribing to the topic (aka. event)
127
+ * @param topic name of topic/event
128
+ * @param callback method which is invoked everytime a message is sent via the topic
129
+ * @returns subscription id used to unsubscribe later
130
+ */
131
+ static subscribeToTopic(topic : string , callback : (data ? : any ) => void ): number
132
+ /**
133
+ * Unsubscribe from any topic (aka. event)
134
+ * @param topic name of topic/event
135
+ * @param subscriptionId subscription id
136
+ */
137
+ static unsubscribeFromTopic(topic : string , subscriptionId : number ): void
138
+ }
139
+ ```
78
140
79
- ### Use Capacitor Plugins
141
+ ## Use Capacitor Plugins
80
142
81
143
Refer [ to this blog post] ( https://blog.nativescript.org/ionic-portals-with-capacitor-plugins ) .
82
144
0 commit comments