SlideShare a Scribd company logo
1 of 37
App創業與實作
App Entrepreneurship and implementation
                       Week 06
        Reusable Code at Clients: Layouts and MVC

                      吳尚鴻
                 (清大資工系專任教授)
                   (2013-03-28)

本授權條款允許使用者重製、散布、傳輸著作,但不得為商業目的之使用,亦不得修改該著作。
使用時必須按照著作人指定的方式表彰其姓名。
CC (Creative Commons)

姓名標示─非商業性─禁止改作

本授權條款允許使用者重製、散布、傳輸著作,但不得為商
業目的之使用,亦不得修改該著作。使用時必須按照著作人
指定的方式表彰其姓名。
Modular JavaScript

    Shan-Hung Wu
      CS, NTHU,
     Spring, 2013
Agenda
• The consequence of complex clients
• Object-oriented JavaScript
• Modular GUI: components, containers, and
  layouts
• Client-side MVC




                                             2
Agenda
• The consequence of complex clients
• Object-oriented JavaScript
• Modular GUI: components, containers, and
  layouts
• Client-side MVC




                                             3
Today’s JavaScript Clients are Very
            Complex




                                      4
Can you write JavaScript like that?




                                      5
The Missing Topics
• Object-oriented JavaScript
  – Writing reusable classes
• Modular GUI: components, containers, and
  layouts
  – Writing assemblable GUI components
• Client-side MVC
  – Maintaining tractable projects


                                             6
Agenda
• The consequence of complex clients
• Object-oriented JavaScript
• Modular GUI: components, containers, and
  layouts
• Client-side MVC




                                             7
Why OOP in JavaScript?




                         8
You Want This
                        Toolbar




          MailToolbar             CalendarToolbar



• Pros:
  – Reusable code in Toolbar


                                                    9
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
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
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
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
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
OK, so how to define a subclass of
User?




                                     15
Classing & Subclassing
1.    var User = function(name) {
2.       this.name = name;
                                                             Object              prototype
3.    };
4.    // define method (why in prototype?)           prototype : object   __proto__ : object
5.    User.prototype.getName = function() {                               constructor: function
6.       return this.name;
7.    }
8.
9.    var usr = new User();                                   User               prototype
10.   alert(usr instanceof User); // true
11.   alert(usr.getName());                          prototype : object   __proto__ : object
12.                                                                       Constructor : function
                                                                          getName : function
13.   // define subclass
14.   Var Vip = function(name, title) {
15.      User.call(this);                                      usr
16.      this.title = title;
17.   }                                              __proto__ : object
                                                     name: string
18.   Vip.prototype = new User();
19.   Vip.prototype.constructor = Vip;
20.   // override method
21.   Vip.prototype.getName = function() {
22.      // if necessary: User.prototype.getName()             Vip               prototype
23.      //       .call(this);
24.      return title + ': ' + name;                 prototype : object   __proto__ : object
25.   }                                                                   constructor: function
                                                                          getName : function

26.   usr = new Vip(...);
27.   alert(usr instanceof Vip); // true                       usr
28.   alert(usr instanceof User); // true            __proto__ : object
29.   alert(usr.getName()); // polymorphism          name: string
                                                     title: string                                 16
Reusing Toolbar




                  17
You Can Do This
                         Toolbar




          MailToolbar              CalendarToolbar


• Toolbar:
  – Basic look, button events
• MailToolbar and CalendarToolbar:
  – Buttons, event handler, and specialized look

                                                     18
Homework
• How to define static members of a class?




                                             19
Agenda
• The consequence of complex clients
• Object-oriented JavaScript
• Modular GUI: components, containers, and
  layouts
• Client-side MVC




                                             20
Components, Containers, and Layouts




2013/3/28                          21
Containing Relationship vs.
                Is-a Relationship
• Containing
  relationship ≠ is-a
  Relationship
• Containing
  relationship
                          Component       Layout
      – Between
        components
      – Visual level
• Is-a relationship       Container

      – Between classes
      – Code level

2013/3/28                                          22
Page Life Cycle
                 • onReady
                 • Init. objects and
                   their members                    Adjust relative sizing & placing




• Load scripts
• Class system                         • Component -> HTML
• Dom Ready                            • Downward recursively

                                                                               23
Initialization
• Right after the onReady() function is called
• Each component and container should be
  constructed and wired together




2013/3/28                                    24
Render
• Convert initialized components and containers
  to html
• Downward along the containing relationship
  by recursively calling(), for example,
  onRender()




2013/3/28                                     25
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
Agenda
• The consequence of complex clients
• Object-oriented JavaScript
• Modular GUI: components, containers, and
  layouts
• Client-side MVC




                                             27
What is MVC?




               28
Why do we need MVC at clients?




                                 29
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
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
Good design?
A component is not reusable anymore
as it is dependent to data



                                      32
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
An Example Project Structure
                               Application name




                                MVC files




Application start point


                                                  34
Q&A




      35

More Related Content

What's hot

Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Addy Osmani
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
안드로이드 데이터 바인딩
안드로이드 데이터 바인딩안드로이드 데이터 바인딩
안드로이드 데이터 바인딩GDG Korea
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritancepedro.carvalho
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocolrocketcircus
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2NAILBITER
 
From C++ to Objective-C
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-Ccorehard_by
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptJulie Iskander
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Ramamohan Chokkam
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends旻琦 潘
 
Exercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera CymbronExercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera Cymbroncymbron
 

What's hot (19)

Iphone course 1
Iphone course 1Iphone course 1
Iphone course 1
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
안드로이드 데이터 바인딩
안드로이드 데이터 바인딩안드로이드 데이터 바인딩
안드로이드 데이터 바인딩
 
Objective-C for Beginners
Objective-C for BeginnersObjective-C for Beginners
Objective-C for Beginners
 
Enterprise js pratices
Enterprise js praticesEnterprise js pratices
Enterprise js pratices
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocol
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2
 
From C++ to Objective-C
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-C
 
Chapter iii(oop)
Chapter iii(oop)Chapter iii(oop)
Chapter iii(oop)
 
Runtime
RuntimeRuntime
Runtime
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
 
YUI Tidbits
YUI TidbitsYUI Tidbits
YUI Tidbits
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Exercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera CymbronExercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera Cymbron
 

Viewers also liked

Ubitus unlocking the_true_potential_of_cloud_gaming
Ubitus unlocking the_true_potential_of_cloud_gamingUbitus unlocking the_true_potential_of_cloud_gaming
Ubitus unlocking the_true_potential_of_cloud_gamingubitus
 
Week 15 - Scaling & Security_Jim Huang
Week 15 - Scaling & Security_Jim HuangWeek 15 - Scaling & Security_Jim Huang
Week 15 - Scaling & Security_Jim HuangAppUniverz Org
 
Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)
Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)
Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)AppUniverz Org
 
Week 17 - Going All The Way_Jason Ho
Week 17 - Going All The Way_Jason Ho Week 17 - Going All The Way_Jason Ho
Week 17 - Going All The Way_Jason Ho AppUniverz Org
 
Week 12 - Ubitus → Google_Victor Shen
Week 12 - Ubitus → Google_Victor ShenWeek 12 - Ubitus → Google_Victor Shen
Week 12 - Ubitus → Google_Victor ShenAppUniverz Org
 
Week 16 - appWorks_IC Jan
Week 16 - appWorks_IC JanWeek 16 - appWorks_IC Jan
Week 16 - appWorks_IC JanAppUniverz Org
 
The0 step 尋找推動成功方程式
The0 step 尋找推動成功方程式The0 step 尋找推動成功方程式
The0 step 尋找推動成功方程式AppUniverz Org
 

Viewers also liked (7)

Ubitus unlocking the_true_potential_of_cloud_gaming
Ubitus unlocking the_true_potential_of_cloud_gamingUbitus unlocking the_true_potential_of_cloud_gaming
Ubitus unlocking the_true_potential_of_cloud_gaming
 
Week 15 - Scaling & Security_Jim Huang
Week 15 - Scaling & Security_Jim HuangWeek 15 - Scaling & Security_Jim Huang
Week 15 - Scaling & Security_Jim Huang
 
Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)
Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)
Week 03 Whoscall's Story_Jeff Kuo (2013 03-07)
 
Week 17 - Going All The Way_Jason Ho
Week 17 - Going All The Way_Jason Ho Week 17 - Going All The Way_Jason Ho
Week 17 - Going All The Way_Jason Ho
 
Week 12 - Ubitus → Google_Victor Shen
Week 12 - Ubitus → Google_Victor ShenWeek 12 - Ubitus → Google_Victor Shen
Week 12 - Ubitus → Google_Victor Shen
 
Week 16 - appWorks_IC Jan
Week 16 - appWorks_IC JanWeek 16 - appWorks_IC Jan
Week 16 - appWorks_IC Jan
 
The0 step 尋找推動成功方程式
The0 step 尋找推動成功方程式The0 step 尋找推動成功方程式
The0 step 尋找推動成功方程式
 

Similar to Week 06 Modular Javascript_Brandon, S. H. Wu

Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part IEugene Lazutkin
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptDonald Sipe
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 
Learn JS concepts by implementing jQuery
Learn JS concepts by implementing jQueryLearn JS concepts by implementing jQuery
Learn JS concepts by implementing jQueryWingify Engineering
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascriptrelay12
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascriptDoeun KOCH
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHPRohan Sharma
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneDeepu S Nath
 

Similar to Week 06 Modular Javascript_Brandon, S. H. Wu (20)

Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
JavsScript OOP
JavsScript OOPJavsScript OOP
JavsScript OOP
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Learn JS concepts by implementing jQuery
Learn JS concepts by implementing jQueryLearn JS concepts by implementing jQuery
Learn JS concepts by implementing jQuery
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Java Constructor
Java ConstructorJava Constructor
Java Constructor
 
Oops in php
Oops in phpOops in php
Oops in php
 
6976.ppt
6976.ppt6976.ppt
6976.ppt
 
Introduction Php
Introduction PhpIntroduction Php
Introduction Php
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
مقدمة بايثون .pptx
مقدمة بايثون .pptxمقدمة بايثون .pptx
مقدمة بايثون .pptx
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHP
 
Javascript
JavascriptJavascript
Javascript
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
 

More from AppUniverz Org

Week 11 - KKBOX_Eric Tsai
Week 11 - KKBOX_Eric TsaiWeek 11 - KKBOX_Eric Tsai
Week 11 - KKBOX_Eric TsaiAppUniverz Org
 
Week 08 Cloud_Eric Shangkuan
Week 08 Cloud_Eric ShangkuanWeek 08 Cloud_Eric Shangkuan
Week 08 Cloud_Eric ShangkuanAppUniverz Org
 
Week 05 Web, App and Javascript_Brandon, S.H. Wu
Week 05 Web, App and Javascript_Brandon, S.H. WuWeek 05 Web, App and Javascript_Brandon, S.H. Wu
Week 05 Web, App and Javascript_Brandon, S.H. WuAppUniverz Org
 
Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)
Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)
Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)AppUniverz Org
 
Appuniverz 回顧與展望
Appuniverz 回顧與展望Appuniverz 回顧與展望
Appuniverz 回顧與展望AppUniverz Org
 
[課程介紹] App創業與實作
[課程介紹] App創業與實作[課程介紹] App創業與實作
[課程介紹] App創業與實作AppUniverz Org
 

More from AppUniverz Org (6)

Week 11 - KKBOX_Eric Tsai
Week 11 - KKBOX_Eric TsaiWeek 11 - KKBOX_Eric Tsai
Week 11 - KKBOX_Eric Tsai
 
Week 08 Cloud_Eric Shangkuan
Week 08 Cloud_Eric ShangkuanWeek 08 Cloud_Eric Shangkuan
Week 08 Cloud_Eric Shangkuan
 
Week 05 Web, App and Javascript_Brandon, S.H. Wu
Week 05 Web, App and Javascript_Brandon, S.H. WuWeek 05 Web, App and Javascript_Brandon, S.H. Wu
Week 05 Web, App and Javascript_Brandon, S.H. Wu
 
Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)
Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)
Week 04 Web Development Flow and Fast Prototyping_hlb (2013-03-14)
 
Appuniverz 回顧與展望
Appuniverz 回顧與展望Appuniverz 回顧與展望
Appuniverz 回顧與展望
 
[課程介紹] App創業與實作
[課程介紹] App創業與實作[課程介紹] App創業與實作
[課程介紹] App創業與實作
 

Recently uploaded

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Recently uploaded (20)

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

Week 06 Modular Javascript_Brandon, S. H. Wu

  • 1. App創業與實作 App Entrepreneurship and implementation Week 06 Reusable Code at Clients: Layouts and MVC 吳尚鴻 (清大資工系專任教授) (2013-03-28) 本授權條款允許使用者重製、散布、傳輸著作,但不得為商業目的之使用,亦不得修改該著作。 使用時必須按照著作人指定的方式表彰其姓名。
  • 3. Modular JavaScript Shan-Hung Wu CS, NTHU, Spring, 2013
  • 4. Agenda • The consequence of complex clients • Object-oriented JavaScript • Modular GUI: components, containers, and layouts • Client-side MVC 2
  • 5. Agenda • The consequence of complex clients • Object-oriented JavaScript • Modular GUI: components, containers, and layouts • Client-side MVC 3
  • 6. Today’s JavaScript Clients are Very Complex 4
  • 7. Can you write JavaScript like that? 5
  • 8. The Missing Topics • Object-oriented JavaScript – Writing reusable classes • Modular GUI: components, containers, and layouts – Writing assemblable GUI components • Client-side MVC – Maintaining tractable projects 6
  • 9. Agenda • The consequence of complex clients • Object-oriented JavaScript • Modular GUI: components, containers, and layouts • Client-side MVC 7
  • 10. Why OOP in JavaScript? 8
  • 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
  • 17. OK, so how to define a subclass of User? 15
  • 18. Classing & Subclassing 1. var User = function(name) { 2. this.name = name; Object prototype 3. }; 4. // define method (why in prototype?) prototype : object __proto__ : object 5. User.prototype.getName = function() { constructor: function 6. return this.name; 7. } 8. 9. var usr = new User(); User prototype 10. alert(usr instanceof User); // true 11. alert(usr.getName()); prototype : object __proto__ : object 12. Constructor : function getName : function 13. // define subclass 14. Var Vip = function(name, title) { 15. User.call(this); usr 16. this.title = title; 17. } __proto__ : object name: string 18. Vip.prototype = new User(); 19. Vip.prototype.constructor = Vip; 20. // override method 21. Vip.prototype.getName = function() { 22. // if necessary: User.prototype.getName() Vip prototype 23. // .call(this); 24. return title + ': ' + name; prototype : object __proto__ : object 25. } constructor: function getName : function 26. usr = new Vip(...); 27. alert(usr instanceof Vip); // true usr 28. alert(usr instanceof User); // true __proto__ : object 29. alert(usr.getName()); // polymorphism name: string title: string 16
  • 20. You Can Do This Toolbar MailToolbar CalendarToolbar • Toolbar: – Basic look, button events • MailToolbar and CalendarToolbar: – Buttons, event handler, and specialized look 18
  • 21. Homework • How to define static members of a class? 19
  • 22. Agenda • The consequence of complex clients • Object-oriented JavaScript • Modular GUI: components, containers, and layouts • Client-side MVC 20
  • 23. Components, Containers, and Layouts 2013/3/28 21
  • 24. Containing Relationship vs. Is-a Relationship • Containing relationship ≠ is-a Relationship • Containing relationship Component Layout – Between components – Visual level • Is-a relationship Container – Between classes – Code level 2013/3/28 22
  • 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
  • 31. Why do we need MVC at clients? 29
  • 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
  • 34. Good design? A component is not reusable anymore as it is dependent to data 32
  • 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
  • 37. Q&A 35