11/*
22Author: Doug Ayers
33Website: https://douglascayers.com
4- GitHub: https://github.com/douglascayers/sfdx-lightning-api
4+ GitHub: https://github.com/douglascayers/sfdx-lightning-api-component
55License: BSD 3-Clause License
66 */
77( {
8- onScriptsLoaded : function ( component , event , helper ) {
9- helper . makeApexRequest ( component , 'c.getVisualforceDomainURL' , { } , { storable : true } ) . then ( function ( vfDomainURL ) {
10- helper . handleOnPostmateScriptsLoaded (
11- component ,
12- component . find ( 'postmate' ) . getElement ( ) ,
13- ` ${ vfDomainURL } /apex/LC_APIPage`
14- ) ;
15- } ) . catch ( $A . getCallback ( function ( err ) {
16- console . error ( 'LC_API: Error in script initialization ' , err ) ;
8+ /**
9+ * Called once during component initialization phase.
10+ */
11+ onInit : function ( component , event , helper ) {
12+ helper . _penpal = { } ;
13+ helper . makeApexRequest ( component , 'c.getVisualforceDomainURL' ) . then ( $A . getCallback ( function ( vfDomainURL ) {
14+ component . set ( 'v.iframeSrc' , ` ${ vfDomainURL } /apex/LC_APIPage` ) ;
15+ } ) ) . catch ( $A . getCallback ( function ( err ) {
16+ console . error ( 'LC_API: Error determining visualforce domain ' , err ) ;
1717 } ) ) ;
1818 } ,
1919
20+ /**
21+ * Called once after ltng:require has loaded scripts.
22+ */
23+ onScriptsLoaded : function ( component , event , helper ) {
24+
25+ } ,
26+
27+ /**
28+ * Called each time the component renders itself.
29+ */
30+ onRender : function ( component , event , helper ) {
31+
32+ let initialized = component . get ( 'v.penpalInitialized' ) ;
33+
34+ // Since the iframe source is calculated asynchronously,
35+ // we listen to the component's render events and each time
36+ // check if the iframe is ready, and if so, then we initialize
37+ // penpal to connect this component to the iframe.
38+ // Since we only want to do this once, we also set the initialized flag.
39+ if ( ! initialized ) {
40+
41+ let iframeElmt = component . find ( 'penpalFrame' ) . getElement ( ) ;
42+
43+ if ( ! $A . util . isEmpty ( iframeElmt . src ) ) {
44+
45+ component . set ( 'v.penpalInitialized' , true ) ;
46+
47+ const connection = Penpal . connectToChild ( {
48+ // The iframe to which a connection should be made
49+ iframe : iframeElmt
50+ } ) ;
51+
52+ helper . _penpal . connection = connection ;
53+
54+ connection . promise . then ( $A . getCallback ( function ( child ) {
55+ // Cache a reference to the child so that we can
56+ // use it in the restRequest/fetchRequest methods,
57+ // as well as be able to destroy it when this component unrenders.
58+ helper . _penpal . child = child ;
59+ } ) ) . catch ( $A . getCallback ( function ( err ) {
60+ console . error ( 'LC_API: Error establishing connection to iframe' , err ) ;
61+ component . set ( 'v.penpalInitialized' , false ) ;
62+ } ) ) ;
63+
64+ } // else, iframe source is empty, keep waiting
65+
66+ }
67+
68+ } ,
69+
2070 onRestRequest : function ( component , event , helper ) {
2171 var params = event . getParam ( 'arguments' ) ;
2272 return helper . handleRestRequest ( component , params . request ) ;
73+ } ,
74+
75+ onFetchRequest : function ( component , event , helper ) {
76+ var params = event . getParam ( 'arguments' ) ;
77+ return helper . handleFetchRequest ( component , params . request ) ;
2378 }
24- } )
79+ } )
80+ /*
81+ BSD 3-Clause License
82+
83+ Copyright (c) 2018, Doug Ayers, douglascayers.com
84+ All rights reserved.
85+
86+ Redistribution and use in source and binary forms, with or without
87+ modification, are permitted provided that the following conditions are met:
88+
89+ * Redistributions of source code must retain the above copyright notice, this
90+ list of conditions and the following disclaimer.
91+
92+ * Redistributions in binary form must reproduce the above copyright notice,
93+ this list of conditions and the following disclaimer in the documentation
94+ and/or other materials provided with the distribution.
95+
96+ * Neither the name of the copyright holder nor the names of its
97+ contributors may be used to endorse or promote products derived from
98+ this software without specific prior written permission.
99+
100+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
101+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
102+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
103+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
104+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
105+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
106+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
107+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
108+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
109+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110+ */
0 commit comments