Javascript para Desenvolvedor PLSQL
Javascript para Desenvolvedor PLSQL
2
Dan McGhan
About
Developer Advocate @ Oracle
Focus on JavaScript and APEX
Contact
[email protected]
@dmcghan
3
Agenda
1 JavaScript Basics
2 Adding JavaScript to APEX Apps
3 Working with jQuery and the DOM
4
Part 1: JavaScript Basics
5
Part 1: JavaScript Basics
1 Why JavaScript?
2 Variables and data types
3 Operators
4 Conditionals and loops
5 Objects and functions
6 Developer tools
6
Part 1: JavaScript Basics
1 Why JavaScript?
7
“ If you're looking for a great APEX
developer, you're really looking for
a full-stack developer.
Joel Kalman, co-creator of APEX
https://joelkallman.blogspot.com/2017/10/a-great-apex-developer-isa-full-stack.html
8
Server-side Client-side
Oracle Database
Data Modeling
SQL
PL/SQL
9
10
11
Server-side Client-side
SQL JavaScript
PL/SQL
12
It often takes just a few lines of JavaScript
to deliver functionality not available out-of-the-box
13
It often takes just a few lines of JavaScript
to deliver functionality not available out-of-the-box
14
It often takes just a few lines of JavaScript
to deliver functionality not available out-of-the-box
15
Custom JavaScript code may break
when upgrading APEX. This often
results from small changes to the
HTML generated by APEX and may
require minor adjustments to fix.
You have been warned!
16
Tips to avoid issues:
Be conservative in assumptions about APEX generated markup
Stick to documented APEX APIs
• https://apex.oracle.com/jsapi
Avoid deprecated APIs
• Includes 3rd party libraries, such as jQuery
17
Languages at a glance
PL/SQL JavaScript
Designed to extend SQL Designed to program the web
3rd generation language 3rd generation language
• Based on Ada • Based on Scheme, C++/Java
Procedural/block structured Flexible/based on functions
18
Part 1: JavaScript Basics
19
Declaring variables in PL/SQL
20
Declaring variables in JavaScript
21
Common data types in PL/SQL
Scalars
All SQL types
NUMBER PLS_INTEGER CHAR
• Too many to list here
VARCHAR2 BOOLEAN DATE
Plus many PL/SQL only types
TIMESTAMP TSWTZ TSWLTZ
• Too many to list here
Cover just about everything! Large Objects
CLOB BLOB BFILE
Composites
Records Collections
Other
NULL
22
Common data types in JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
23
Common data types in JavaScript
https://www.youtube.com/watch?v=wPBjd-vb9eI
24
Two syntaxes for creating new objects
Object literal
• Simpler than a constructor function
• Supported by all primitives and some objects
• Date not supported
Constructor function
• Uses new keyword with constructor function
• Use when object literal not available
25
Part 1: JavaScript Basics
3 Operators
26
Common operator types
27
Assignment operators overview
PL/SQL JavaScript
Simple assignment := =
Add and assign +=
Subtract and assign -=
Multiply and assign *=
Divide and assign /=
Modulus and assign %=
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators
28
Using the PL/SQL assignment operator
29
JavaScript assignment operators
30
Arithmetic operators overview
PL/SQL JavaScript
Addition + +
Subtraction - -
Multiplication * *
Division / /
Modulus mod(m, n) %
Increment ++
Decrement --
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
31
Arithmetic operators overview
PL/SQL JavaScript
Addition + +
Subtraction - -
Multiplication * *
+ is also the concatenation operator! 🤪
Division / /
Modulus mod(m, n) %
Increment ++
Decrement --
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
32
Using PL/SQL arithmetic operators
33
Using JavaScript arithmetic operators
34
Comparison operators overview
PL/SQL JavaScript
Equal to = ==
Equal to value and type ===
Not equal to != or <> !=
Not equal to value or type !==
Greater than > >
Less than < <
Greater than or equal to >= >=
Less than or equal to <= <=
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
35
Using PL/SQL comparison operators
36
Using JavaScript comparison operators
37
Using JavaScript comparison operators
38 https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/getting-started/ch2.md#equalish
Logical operators overview
PL/SQL JavaScript
And and &&
Or or ||
Not not !
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
39
Using PL/SQL logical operators
40
Using JavaScript logical operators
41
Part 1: JavaScript Basics
42
Conditional logic overview
PL/SQL JavaScript
If-then-else Yes Yes
Case Yes No
Switch No Yes
Ternary operator No Yes
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals
43
Using PL/SQL if-then-else & case statements
If-then-else
44
Using PL/SQL if-then-else & case statements
If-then-else Case
45
Using JavaScript if-then-else & switch statements
If-then-else
46
Using JavaScript if-then-else & switch statements
If-then-else Switch
47
Using JavaScript if-then-else & switch statements
If-then-else Switch
Be careful with
“fall through”
48
Using JavaScript’s ternary operator
49
Truthy and falsy values in JavaScript
50
Loops overview
PL/SQL JavaScript
Loop Yes No
For loop Yes Yes
Cursor for loop Yes No
While loop Yes Yes
Do while loop No Yes
For in loop Yes Yes
For of loop No Yes
Loop labels Yes Yes
Continue Yes Yes
51 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration
Basic for loop comparison
PL/SQL
52
Basic for loop comparison
PL/SQL JavaScript
53
Part 1: JavaScript Basics
54
Basic “object” comparison
PL/SQL
55 https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics
Basic “object” comparison
PL/SQL JavaScript
56 https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics
Functions overview
PL/SQL JavaScript
Functions Yes (must return a value) Yes (optionally return a value)
Procedures Yes (do not return a value) No (use a function with no return value)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
57
Basic function comparison
PL/SQL
58
Basic function comparison
PL/SQL JavaScript
59
Using functions in JavaScript
60
Creating functions in JavaScript
Function statement
61
Creating functions in JavaScript
62
JavaScript objects/functions in browsers
Object Description
window THE “global” object. Represents the browser window. (think SYS schema)
document API to the document/web page. We’ll cover this later.
console API for debug output. (think dbms_output)
Math Provides many mathematical properties and functions.
JSON Object that provides tools for working with JSON, e.g. JSON.parse & JSON.stringify.
setTimeout Function to schedule work for the future (think dbms_scheduler)
setInterval Function to schedule recurring work (think dbms_scheduler)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference#Global_Objects
63
Pop quiz!
Which of the following will not throw an exception?
A B C
64
Part 1: JavaScript Basics
6 Developer tools
65
Developer tools for PL/SQL
66
Developer tools for JavaScript
67 https://developers.google.com/web/tools/chrome-devtools
Part 2: Adding JavaScript to APEX Apps
68
Part 2: Adding JavaScript to APEX Apps
1 Dynamic Actions
2 Dynamic Actions with JavaScript snippets
3 Page and component level attributes
4 External files
69
Part 2: Adding JavaScript to APEX Apps
1 Dynamic Actions
70
What are Dynamic Actions?
71
Thinking through Dynamic Actions
Event
Action
72
Thinking through Dynamic Actions
Event
Action
Action
Action
73
Thinking through Dynamic Actions
When Event
Action
Actions Action
Action
74
Thinking through Dynamic Actions
False True
75
Thinking through Dynamic Actions
False True
Action Action
Action Action
76
Thinking through Dynamic Actions
False True
Action Action
Action Action
77
Example
78
Example
79
Example
False True
80
Example
False True
81
Part 2: Adding JavaScript to APEX Apps
82
Dynamic Actions with JavaScript snippets
83
When: Event and Selection Type
84
When: Client-side Condition
85
Actions: Execute JavaScript
86
Part 2: Adding JavaScript to APEX Apps
87
Page level attributes
88
Using page level attributes
89
Part 2: Adding JavaScript to APEX Apps
4 External files
90
External files
91
Using external files on a page
92
Using external files throughout an app
93
See the JS doc for additional info on adding JavaScript to APEX
94 https://apex.oracle.com/jsapi
Part 3: Working with jQuery and the DOM
95
Part 3: Working with jQuery and the DOM
96
Part 3: Working with jQuery and the DOM
97
HTML vs. DOM
98
HTML document
http://www.html5rocks.com/en/tutorials/internals/howbrowserswork
99
DOM Tree
http://www.html5rocks.com/en/tutorials/internals/howbrowserswork
100
The DOM in JavaScript
Endless
Web
JS + APIs = Possibilities!
😃
https://www.w3.org/TR/?tag=webapi
101
Part 3: Working with jQuery and the DOM
2 What is jQuery?
102
DOM problems
103
jQuery
104
Using jQuery
105
Part 3: Working with jQuery and the DOM
106
Basic selectors
http://api.jquery.com/category/selectors/
107
DOM elements vs. jQuery objects
108
Example web page
https://en.wikipedia.org/wiki/Paul_Ekman
109
Selection
<div class="question-wrapper">
ID <div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
Class <li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
Element </ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
110
Selection
$("#question")
<div class="question-wrapper">
ID <div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
Class <li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
Element </ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
111
Selection
$("#emotions-list")
<div class="question-wrapper">
ID <div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
Class <li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
Element </ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
112
Selection
$(".positive")
<div class="question-wrapper">
ID <div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
Class <li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
Element </ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
113
Selection
$(".negative")
<div class="question-wrapper">
ID <div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
Class <li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
Element </ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
114
Selection
$("div")
<div class="question-wrapper">
ID <div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
Class <li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
Element </ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
115
Selection
$("input")
<div class="question-wrapper">
ID <div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
Class <li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
Element </ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
116
Simple traversing
http://api.jquery.com/category/traversing/
117
Traversal
$("#question")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
118
Traversal
$("#question").parent()
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
119
Traversal
$("#question").parent().find("li")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
120
Traversal
$("#question").parent().find("li").eq(2)
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
121
Simple DOM manipulation
http://api.jquery.com/category/manipulation/
122
Manipulation
$(".neutral")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
123
Manipulation
$(".neutral").addClass("positive")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral positive">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
124
Manipulation
$(".neutral").addClass("positive").removeClass("neutral")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="positive">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
125
Manipulation
$("input[type='text']")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="positive">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
126
Manipulation
$("input[type='text']").attr("disabled", "disabled")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="positive">Surprised</li>
</ul>
</div>
<input type="text" name="feeling"
disabled="disabled">
<input type="button" value="Submit">
</div>
127
Manipulation
$("#question")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
128
Manipulation
<div class="question-wrapper">
<div><h1 id="question">How do you feel?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
129
Manipulation
$("#emotions-list")
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
130
Manipulation
$("#emotions-list").append('<li class="positive">Amusement</li>')
<div class="question-wrapper">
<div><h1 id="question">How are you feeling?</h1>
<div>Here are the six basic emotions:</div>
<ul id="emotions-list">
<li class="positive">Happy</li>
<li class="negative">Sad</li>
<li class="negative">Fearful</li>
<li class="negative">Disgusted</li>
<li class="negative">Angry</li>
<li class="neutral">Surprised</li>
<li class="positive">Amusement</li>
</ul>
</div>
<input type="text" name="feeling">
<input type="button" value="Submit">
</div>
131
Part 3: Working with jQuery and the DOM
4 Events overview
132
What are events?
133
APEX Browser Events
134
DOM/Element events
https://docs.oracle.com/en/database/oracle/application-express/19.1/htmdb/managing-dynamic-actions.html
135
Keyboard events
https://docs.oracle.com/en/database/oracle/application-express/19.1/htmdb/managing-dynamic-actions.html
136
Mouse/Trackpad events
https://docs.oracle.com/en/database/oracle/application-express/19.1/htmdb/managing-dynamic-actions.html
137
Finger/Pointer events
https://docs.oracle.com/en/database/oracle/application-express/19.1/htmdb/managing-dynamic-actions.html
138
Part 3: Working with jQuery and the DOM
139
Binding with on()
https://api.jquery.com/on/
140
Functions are “first-class” in JavaScript
141
Functions are “first-class” in JavaScript
$('#input-test').on('change', handleChange);
</script>
142
Pop quiz!
What’s the difference between the two event bindings?
A B
143
Window load vs. DOM content load
https://api.jquery.com/ready/#ready-handler
144
Event dispatching and DOM event flow
https://www.w3.org/TR/DOM-Level-3-Events/#event-flow
145
Event delegation with on()
$('.report-button').on('click', function() {
console.log('direct binding');
});
146
Event delegation with on()
$('.report-button').on('click', function() {
console.log('direct binding');
});
147
Event delegation with on()
$('.report-button').on('click', function() {
console.log('direct binding');
});
$('#report').on('click', '.report-button', function() {
console.log('delegated binding');
});
148
Recap
1 JavaScript Basics
2 Adding JavaScript to APEX Apps
3 Working with jQuery and the DOM
149
Next steps…
150