WebFund 2015W Lecture 15: Difference between revisions
Created page with "==Video== The video from the lecture given on March 4, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec15-04Mar2015.mp4 is now availab..." |
No edit summary |
||
(2 intermediate revisions by one other user not shown) | |||
Line 5: | Line 5: | ||
==Code== | ==Code== | ||
Files from [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/code/persistent-notes.zip persistent-notes] that were changed/added: | |||
===public/javascripts/notes.js=== | |||
<source lang="javascript"> | <source lang="javascript"> | ||
$(function() { | $(function() { | ||
Line 15: | Line 17: | ||
===views/layout.jade=== | |||
<source lang="html5"> | |||
doctype html | |||
html | |||
head | |||
title= title | |||
link(rel='stylesheet', href='/stylesheets/style.css') | |||
script(src="//code.jquery.com/jquery-1.11.2.min.js") | |||
script(src="//code.jquery.com/jquery-migrate-1.2.1.min.js") | |||
block header | |||
body | |||
block content | |||
</source> | |||
===views/notes.jade=== | |||
<source lang="html5"> | |||
extends layout | |||
block header | |||
script(src="/javascripts/notes.js") | |||
block content | |||
h1 #{username} Account | |||
p Welcome #{username} | |||
p#otheruser | |||
- if(error) | |||
div.error #{error} | |||
form(action="/logout", method="post") | |||
button(type="submit") Logout | |||
h2 Notes | |||
ul | |||
- for (i=0; i < userNotes.length; i++) | |||
li | |||
div | |||
b= userNotes[i].title + ": " | |||
-var note_url = "/edit/" + userNotes[i]._id | |||
a(href=note_url) [edit] | |||
p!= userNotes[i].content | |||
form(action="/new", method="post") | |||
button(type="submit") New Note | |||
</source> | |||
===routes/index.js=== | |||
<source lang="javascript"> | |||
// add before modules.exports line at the end | |||
router.get("/testdata", function(req, res) { | |||
res.send({"username": "Bob"}); | |||
}); | |||
</source> | |||
==Notes== | ==Notes== | ||
<h3>On-screen notes</h3> | |||
<pre> | <pre> | ||
AJAX | AJAX | ||
Line 50: | Line 111: | ||
- callback | - callback | ||
</pre> | </pre> | ||
<h3>AJAX</h3> | |||
AJAX stands for Asynchronous JavaScript And XML. Sometimes it's written as AJAJ instead when JSON is used in place of XML. | |||
Old JavaScript ran only in response to local events like timers, mouse and keyboard input, and the page loading. | |||
AJAX allows background processing, particularly of network communication. It enables you to do GETs and POSTs in the background, allowing for 'single page' web apps that don't require constant reloading. This is in contrast to older | |||
web apps, which had to reload any time an action changed the state of the page (bad GUI design!). AJAX is based on XMLHttpRequest - originally an ActiveX object. It was written by Microsoft for Outlook Web Access to implement | |||
additional functionality. It is now a native feature. | |||
jQuery provides nicer interfaces to XMLHttpRequest. The jQuery methods we're interested in: | |||
<ul> | |||
<li> <code>$.getJSON()</code> : requests a JavaScript object from the server</li> | |||
<li> <code>$.post()</code> : sends a JavaScript object to the server</li> | |||
</ul> | |||
The arguments for these methods consist of a URL (the path on the server), and a callback function. We don't know how long the server will take to respond, so callbacks are necessary! | |||
<h3>Coding with jQuery</h3> | |||
The $ sign is a substitution for "jQuery". | |||
To add jQuery to a Jade document, we can add the following lines: | |||
<code><pre> | |||
script(src="//code.jquery.com/jquery-1.11.2.min.js") | |||
script(src="//code.jquery.com/jquery-migrate-1.2.1.min.js") | |||
</pre></code> | |||
Similarly, to add client-side code: | |||
<code>script(src="/javascripts/notes.js")</code> | |||
These scripts should be placed in the head of the document. The code in notes.js will be run once the page is fully loaded. Generally, for scripts running on the client side, all the code should be contained inside a function | |||
for the 'on page load' event, like so: | |||
<code><pre> | |||
$(function() { | |||
$.getJSON("/testdata", function(data) { | |||
$("#otheruser").text("Or is it " + data.username + "?"); | |||
}); | |||
}); | |||
</pre></code> | |||
<code>$.getJSON("/testdata", function(data) {...}</code> simply translates to a GET request. If the GET request fails, the code in function will not run. testdata does not need to be a physical file. We can define it as a route in | |||
index.js: | |||
<code><pre> | |||
router.get("/testdata", function(req, res) { | |||
res.send({"username": "Bob"});}); | |||
</pre></code> | |||
To access an element in our Jade code from notes.js, we first want to give the element an ID. In Jade, this is done by prefacing it with the # sign: <code>p#otheruser</code>. This is semantically equivalent to <code><p id = "otheruser"></code> in HTML. Then we can query it from notes.js like so: | |||
<code>$("#otheruser").text("Or is it " + data.username + "?");</code> | |||
The indicated text will be loaded in place of <code>#otheruser</code> after the page has loaded. | |||
Changes made to the DOM by client side scripts will not be visible when viewed from the page source, as the source only shows what was received from the server. The above code will appear as | |||
<code><p id = "otheruser"></code> in the page source rather than the change indicated in notes.js. The element inspector, however, does show changes made on the client side. |
Latest revision as of 01:36, 12 March 2015
Video
The video from the lecture given on March 4, 2015 is now available.
Code
Files from persistent-notes that were changed/added:
public/javascripts/notes.js
$(function() {
$.getJSON("/testdata", function(data) {
$("#otheruser").text("Or is it " + data.username + "?");
});
});
views/layout.jade
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
script(src="//code.jquery.com/jquery-1.11.2.min.js")
script(src="//code.jquery.com/jquery-migrate-1.2.1.min.js")
block header
body
block content
views/notes.jade
extends layout
block header
script(src="/javascripts/notes.js")
block content
h1 #{username} Account
p Welcome #{username}
p#otheruser
- if(error)
div.error #{error}
form(action="/logout", method="post")
button(type="submit") Logout
h2 Notes
ul
- for (i=0; i < userNotes.length; i++)
li
div
b= userNotes[i].title + ": "
-var note_url = "/edit/" + userNotes[i]._id
a(href=note_url) [edit]
p!= userNotes[i].content
form(action="/new", method="post")
button(type="submit") New Note
routes/index.js
// add before modules.exports line at the end
router.get("/testdata", function(req, res) {
res.send({"username": "Bob"});
});
Notes
On-screen notes
AJAX ---- Asynchronous JavaScript And XML more AJAJ (XML => JSON) Old JavaScript just ran in response to local events - timer - mouse, keyboard - on page load AJAX allows background processing, particularly of network communication - do GETs and POSTs in the background Allows for "single page" web apps XMLHttpRequest - originally an ActiveX object - now native - written by Microsoft for Outlook Web Access jQuery provides nicer interfaces to XMLHttpRequest * $.getJSON() * $.post() Arguments: - URL (path on the server) - callback
AJAX
AJAX stands for Asynchronous JavaScript And XML. Sometimes it's written as AJAJ instead when JSON is used in place of XML.
Old JavaScript ran only in response to local events like timers, mouse and keyboard input, and the page loading.
AJAX allows background processing, particularly of network communication. It enables you to do GETs and POSTs in the background, allowing for 'single page' web apps that don't require constant reloading. This is in contrast to older web apps, which had to reload any time an action changed the state of the page (bad GUI design!). AJAX is based on XMLHttpRequest - originally an ActiveX object. It was written by Microsoft for Outlook Web Access to implement additional functionality. It is now a native feature.
jQuery provides nicer interfaces to XMLHttpRequest. The jQuery methods we're interested in:
-
$.getJSON()
: requests a JavaScript object from the server -
$.post()
: sends a JavaScript object to the server
The arguments for these methods consist of a URL (the path on the server), and a callback function. We don't know how long the server will take to respond, so callbacks are necessary!
Coding with jQuery
The $ sign is a substitution for "jQuery".
To add jQuery to a Jade document, we can add the following lines:
script(src="//code.jquery.com/jquery-1.11.2.min.js")
script(src="//code.jquery.com/jquery-migrate-1.2.1.min.js")
Similarly, to add client-side code:
script(src="/javascripts/notes.js")
These scripts should be placed in the head of the document. The code in notes.js will be run once the page is fully loaded. Generally, for scripts running on the client side, all the code should be contained inside a function for the 'on page load' event, like so:
$(function() {
$.getJSON("/testdata", function(data) {
$("#otheruser").text("Or is it " + data.username + "?");
});
});
$.getJSON("/testdata", function(data) {...}
simply translates to a GET request. If the GET request fails, the code in function will not run. testdata does not need to be a physical file. We can define it as a route in
index.js:
router.get("/testdata", function(req, res) {
res.send({"username": "Bob"});});
To access an element in our Jade code from notes.js, we first want to give the element an ID. In Jade, this is done by prefacing it with the # sign: p#otheruser
. This is semantically equivalent to <p id = "otheruser">
in HTML. Then we can query it from notes.js like so:
$("#otheruser").text("Or is it " + data.username + "?");
The indicated text will be loaded in place of #otheruser
after the page has loaded.
Changes made to the DOM by client side scripts will not be visible when viewed from the page source, as the source only shows what was received from the server. The above code will appear as
<p id = "otheruser">
in the page source rather than the change indicated in notes.js. The element inspector, however, does show changes made on the client side.