SRCmp.cmp
- Open Developer Console.
- Go to New > Aura Component
- Enter Name
SRCmp
- Hit Submit
- Click on the
Component
button in the side panel. - Paste the following content:
<aura:component> <aura:attribute name="title" type="String" default="Modal"/> <!-- listen to staticResourcesLoaded and call "initScripts" function inside controller --> <aura:handler event="jam:staticResourcesLoaded" action="{!c.initScripts}"/> <div class="aotp"> <div class="container"> <!-- Button triggers a modal dialog--> <div class="row well"> <button aura:id="modalToggle" class="btn btn-primary btn-lg"> Launch Modal - JavaScript </button> </div> <br /> <!-- modal dialog --> <div class="modal fade" aura:id="modalDlg"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title">Dialog Title</h4> </div> <div class="modal-body"> GlobalId = {!globalId} <br/> This is Dialog Body </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> <!-- /.modal-content --> </div> <!-- /.modal-dialog --> </div> <!-- /.modal --> </div> </div></aura:component>
- Change the namespace “jam” to your org’s namespace
Code Highlights:
<aura:handler event="jam:staticResourcesLoaded" action="{!c.initScripts}"/>
This makes this component listen tojam:staticResourcesLoaded
event and callinitScripts
function in its JavaScript controller.The rest of the code is simply markup to create a Twitter Bootstrap button and a Modal dialog.
<button aura:id="modalToggle" class="btn btn-primary btn-lg">
aura:id
with a valuemodalToggle
makes it easy to find from within component. It is recommended to useaura:id
instead of regularid
because your app may have multiple components.
SRController.js
- Open
SRCmp.cmp
and click onController
button in the side panel. - Paste the following code.
({ initScripts: function(component, event, helper) { //Ignore duplicate notifications that may arrive because other components //loading scripts using the same library. if (component.alreadyhandledEvent) return; var btn = component.find("modalToggle").getElement(); var dlg = component.find("modalDlg").getElement(); jQuery(btn).on("click", function() { jQuery(dlg).modal(); }); component.alreadyhandledEvent = true; } }})
Application-Level EventsNote: If you are listening to
Application level
events, you should guard your code from duplicate notifications!
SRApp.app
- Open Developer Console.
- Go to New > Aura Application
- Enter Name
SRApp
- Hit Submit
- Click on the
Application
button in the side panel. - Paste the following content:
<aura:application> <!-- 1. Loads aotp_bootstrap.css from a aotp_bootstrap(zip file) and loads jQuery file (not in zip file; notice "sfjs" extension) in parallel. And then loads bootstrap.js from aotp_bootstrap(zip file) as bootstrap.js is dependent on jQuery. --> <jam:load filesInParallel="/resource/aotp_bootstrap/css/aotp_bootstrap.css,/resource/jquery.sfjs" filesInSeries="/resource/aotp_bootstrap/js/bootstrap.js" /> <div>1st copy of the component</div> <NAMESPACE:SRCmp/> <div>2nd copy of the component</div> <NAMESPACE:SRCmp/></aura:application>
Test The App
- Click on the convenient “Preview” button on the side bar to open up the app - You should see Twitter Bootstrap theme.
- Click on the button and you should see a modal dialog triggered by jQuery.
- Since we are guarding against multiple notifications, both buttons should work normally.