11. You Want This
Toolbar
MailToolbar CalendarToolbar
• Pros:
– Reusable code in Toolbar
9
12. Function Objects: A Review
• In JavaScript, functions are objects
1. var User = function() {
2. this.name = ...
3. };
4. // as object
5. User.name = ...;
6. // as function
7. var ret = User();
8. // as constructor
9. var usr = new User();
• What does it really mean?
10
13. Homework: the memory scheme?
1. var obj = new Object();
2. obj.x = 1;
3. obj.y = 2;
4.
5. var User = function(name) {
6. this.name = name;
7. };
8. var usr = new User();
11
14. Memory Scheme
1. var obj = new Object(); Object prototype
2. obj.x = 1;
prototype : object __proto__ : object
3. obj.y = 2;
constructor: function
4.
5. var User = function(name) {
6. this.name = name; obj
7. }; __proto__ : object
8. var usr = new User(); x: number
y: number
• __proto__ is not
accessible directly User prototype
– Only through prototype : object __proto__ : object
Object.prototype constructor: function
• Different objs reference
usr
the same prototype
object __proto__ : object
name: string
12
15. Prototype Chain
1. var obj = new Object(); Object prototype
2. obj.x = 1;
prototype : object __proto__ : object
3. obj.y = 2;
constructor: function
4.
5. var User = function(name) {
6. this.name = name; obj
7. }; __proto__ : object
8. var usr = new User(); x: number
y: number
• Members of an
object are sought User prototype
along the prototype prototype : object __proto__ : object
constructor: function
chain from bottom
up usr
– The first reached __proto__ : object
wins name: string
13
16. OOP Goals
• Inheritance
– The ability to define the behavior of one object in terms of
another by sub-classing
• Polymorphism
– The ability for two classes to respond to the same
(collection of) methods
• Encapsulation
– Hidden details via private members
– Not recommended since
• Bad performance
• The benefits of using private members is rather limited in the
context of JavaScript (which is already lacking any form of type
safety)
14
20. You Can Do This
Toolbar
MailToolbar CalendarToolbar
• Toolbar:
– Basic look, button events
• MailToolbar and CalendarToolbar:
– Buttons, event handler, and specialized look
18
25. Page Life Cycle
• onReady
• Init. objects and
their members Adjust relative sizing & placing
• Load scripts
• Class system • Component -> HTML
• Dom Ready • Downward recursively
23
26. Initialization
• Right after the onReady() function is called
• Each component and container should be
constructed and wired together
2013/3/28 24
27. Render
• Convert initialized components and containers
to html
• Downward along the containing relationship
by recursively calling(), for example,
onRender()
2013/3/28 25
28. Layout
• Positioning, sizing, and CSS calculation
between each container and its containing
components
– Upward recursively
– Done by the layout object associated with each
container
• Every time the layout changed, DOM will re-
compute all elements in the site.
2013/3/28 26
29. Agenda
• The consequence of complex clients
• Object-oriented JavaScript
• Modular GUI: components, containers, and
layouts
• Client-side MVC
27
32. Think about
Data
• Despite of protocols
(SOAP, REST, etc.), you
want to store data
obtained from server
– Saves time if two components share the same
data
– Also reduces server load
• What if one component change the data?
– How to notify another component?
30
33. Trick
1. Define JS objects that represent/wrap the
data
2. Allow these objects to fire events (e.g.,
data_changed(oldVal, newVal))
3. Let Component objects listen to these data
events
31
35. Client-side MVC
• Model is a collection of objects reflecting data and their
fields
– Fire data-related events
– Relational model at client-side
– Can define advanced classes to ease data manipulation, e.g.,
searcher
• View is a collection of component
– E.g., Grids, trees, toolbar, etc.
– Fires view-related events
• Controllers bind views and models together
– Listen to events from both views and models and handle them
• Pros?
33
36. An Example Project Structure
Application name
MVC files
Application start point
34