WebFund 2015W Lecture 16: Difference between revisions
Created page with "==Video== The video from the lecture given on March 9, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec16-09Mar2015.mp4 is now availab..." |
No edit summary |
||
Line 2: | Line 2: | ||
The video from the lecture given on March 9, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec16-09Mar2015.mp4 is now available]. | The video from the lecture given on March 9, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec16-09Mar2015.mp4 is now available]. | ||
==Notes== | |||
<h3>AJAX-notes</h3> | |||
Assignment 7 is similar to assignment 6 but using we are now using the AJAX-notes demo application (code is very different but logic is same). | |||
Notice that the URL does not change when moving through the different notes pages. This is due to the use of client-side JavaScript. If we look at the DOM through the element inspector in a web browser, the code is much more complex than the page source. This too is due to the use of client-side JavaScript. When actions are made such as moving to the edit note view, the client-side JavaScript modifies the DOM now rather than having the client render a whole new page. | |||
<h4>Routing changes</h4> | |||
To get the user's notes, we use a new route called /getNotes. This returns note objects in an array (passed as JSON string). It is parsed by the browser to generate the notes list. If you put this route directly into the browser's address bar, you can see the JSON string representation of the response which is simply all of the user notes. | |||
Other differences in index.js include: | |||
<ul> | |||
<li>'/notes' - This route no longer provides the notes to the Jade page which gets rendered. The retrieval of the notes is now handled in the client-side script.</li> | |||
<li>'/edit/:id' - This route no longer exists. This functionality has been moved to the client-side script as well.</li> | |||
<li>'/updateNote' - This is now used instead of the '/update' route.</li> | |||
</ul> | |||
<h4>Jade changes</h4> | |||
The Jade code is now much simpler. The Jade templates for the edit page, the delete page and the updated page are no longer needed. All of this is replaced by the functionality achieve on the single notes page through the client-side script. The contents of notes.jade is also simpler. Much of the content is now replaced by a <code>div.notesArea</code>. The notes.js script appends conent in this div. | |||
<h4>The client-side script</h4> | |||
All of the moved functionality can be found in the script notes.js. This is a client-side script which is included in the head of the notes page using <code>script(src=”/javascript/notes.js”) </code>. This script makes use of jQuery to manipulate the DOM. The script starts by running everything inside <code>$(function(){...}</code> when the page has finished loading. Initially, a bunch of functions are set up, then at the end, one of them is called. | |||
The noteListing() function: | |||
<ul> | |||
<li>Replaces the notesArea div with a new div.</li> | |||
<li>Appends some HTML elements to the div.</li> | |||
<li>Inserts a placeholder saying "Loading Notes..."</li> | |||
<li>Adds a New Note and a Refresh button, giving them a each a click event.</li> | |||
</ul> | |||
The getAndListNotes() function: | |||
<ul> | |||
<li>Sends a GET request to the server to the '/getNotes' route.</li> | |||
<li>Calls another function to populate the notes with and passes it the data received from the server's response.</li> | |||
</ul> | |||
The GET request in getAndListNotes() as well as other requests used in this script all happen in the background (asynchronously) and do not require the page to ever be reloaded. They simply update the DOM as necessary. | |||
<h3>Client-side JavaScript calculator</h3> | |||
A simple example such as the calculator shown in class may be used as part of the course later. | |||
Some aspects of this calculator example include: | |||
<ul> | |||
<li>The page is never reloaded - all operations are cient-side</li> | |||
<li>The source code is quite simple</li> | |||
<li><code>digits.not($(‘#dot, #sgin’)).click</code> is used to prevent the default event from happening</li> | |||
</ul> | |||
<h3>On-screen notes</h3> | |||
AJAX | |||
<ul> | |||
<li>client-side JavaScript | |||
<li>uses background GETs and Posts | |||
</ul> | |||
What else do “major” web apps do? | |||
<ul> | |||
<li>nothing else, fundamentally</li> | |||
<li>use more APIs | |||
<ul> | |||
<li>graphics (canvas)</li> | |||
<li>socket I/O</li> | |||
<li>...</li> | |||
</ul> | |||
</li> | |||
<li>fancier libraries | |||
<ul> | |||
<li>higher-level interface</li> | |||
<li>handle page updates transparently | |||
<ul> | |||
<li>change data on server, pushed to browser</li> | |||
<li>change data in browser, pushed to server</li> | |||
</ul> | |||
</li> | |||
</ul> | |||
</li> | |||
<li>different languages | |||
<ul> | |||
<li>e.g. write in Java, deploy in JavaScript</li> | |||
<li>asm.js (subset of JavaScript) | |||
<ul> | |||
<li>write in c or c++ and compile to JavaScript</li> | |||
</ul> | |||
</li> | |||
</ul> | |||
</li> | |||
</ul> |
Latest revision as of 02:26, 16 March 2015
Video
The video from the lecture given on March 9, 2015 is now available.
Notes
AJAX-notes
Assignment 7 is similar to assignment 6 but using we are now using the AJAX-notes demo application (code is very different but logic is same).
Notice that the URL does not change when moving through the different notes pages. This is due to the use of client-side JavaScript. If we look at the DOM through the element inspector in a web browser, the code is much more complex than the page source. This too is due to the use of client-side JavaScript. When actions are made such as moving to the edit note view, the client-side JavaScript modifies the DOM now rather than having the client render a whole new page.
Routing changes
To get the user's notes, we use a new route called /getNotes. This returns note objects in an array (passed as JSON string). It is parsed by the browser to generate the notes list. If you put this route directly into the browser's address bar, you can see the JSON string representation of the response which is simply all of the user notes.
Other differences in index.js include:
- '/notes' - This route no longer provides the notes to the Jade page which gets rendered. The retrieval of the notes is now handled in the client-side script.
- '/edit/:id' - This route no longer exists. This functionality has been moved to the client-side script as well.
- '/updateNote' - This is now used instead of the '/update' route.
Jade changes
The Jade code is now much simpler. The Jade templates for the edit page, the delete page and the updated page are no longer needed. All of this is replaced by the functionality achieve on the single notes page through the client-side script. The contents of notes.jade is also simpler. Much of the content is now replaced by a div.notesArea
. The notes.js script appends conent in this div.
The client-side script
All of the moved functionality can be found in the script notes.js. This is a client-side script which is included in the head of the notes page using script(src=”/javascript/notes.js”)
. This script makes use of jQuery to manipulate the DOM. The script starts by running everything inside $(function(){...}
when the page has finished loading. Initially, a bunch of functions are set up, then at the end, one of them is called.
The noteListing() function:
- Replaces the notesArea div with a new div.
- Appends some HTML elements to the div.
- Inserts a placeholder saying "Loading Notes..."
- Adds a New Note and a Refresh button, giving them a each a click event.
The getAndListNotes() function:
- Sends a GET request to the server to the '/getNotes' route.
- Calls another function to populate the notes with and passes it the data received from the server's response.
The GET request in getAndListNotes() as well as other requests used in this script all happen in the background (asynchronously) and do not require the page to ever be reloaded. They simply update the DOM as necessary.
Client-side JavaScript calculator
A simple example such as the calculator shown in class may be used as part of the course later.
Some aspects of this calculator example include:
- The page is never reloaded - all operations are cient-side
- The source code is quite simple
digits.not($(‘#dot, #sgin’)).click
is used to prevent the default event from happening
On-screen notes
AJAX
- client-side JavaScript
- uses background GETs and Posts
What else do “major” web apps do?
- nothing else, fundamentally
- use more APIs
- graphics (canvas)
- socket I/O
- ...
- fancier libraries
- higher-level interface
- handle page updates transparently
- change data on server, pushed to browser
- change data in browser, pushed to server
- different languages
- e.g. write in Java, deploy in JavaScript
- asm.js (subset of JavaScript)
- write in c or c++ and compile to JavaScript