Skip to Content
JAVASCRIPT網頁應用程式設計
book

JAVASCRIPT網頁應用程式設計

by Alex MacCaw
January 2013
Intermediate to advanced
332 pages
5h 23m
Chinese
GoTop Information, Inc.
Content preview from JAVASCRIPT網頁應用程式設計
第五章
檢視器(View)與模版
Template
檢視器(View),指的是應用程式的操作介面,也就是使用者會實際看到並與之互動的
部分。檢視器裡最好只有單純的 HTML 程式碼,而且不應包含任何程式邏輯。所有和
檢視器相關的程式邏輯(像是設定事件處理器,以及增刪資料),都應該要交由控制器
Controller)負責。很多時候為了貪圖一時之快,難免都會有想要違反 MVC 規範、將
程式邏輯直接寫進檢視器裡的衝動。請你千萬要克制住這股衝動!否則程式碼最後勢必
會變得一團混亂。
在我們把伺服器端應用程式,改寫為用戶端應用程式的過程中,檢視器應該算是程式架
構需要更動最多的地方之一。以往在開發伺服器端應用程式時,都會直接把伺服器端的
資料變數寫進 HTML 程式碼裡,然後在執行程式時由伺服器動態產生新的網頁內容。但
JavaScript 應用程式裡的檢視器,則會採取略微不同的概念,來進行網頁呈現的流程。
第一步,是把檢視器需要用到的資料通通傳回用戶端(因為你沒辦法直接存取伺服器端
的變數)。一般的做法是發送 Ajax 請求給伺服器,然後再透過應用程式的模型(Model
來載入伺服器所回傳的 JSON 物件。至於預先產生 HTML 程式碼的工作,則不再由伺
服器來處理,而是全權交給用戶端負責。這麼做的好處,是能讓用戶端應用程式不需仰
賴伺服器來產生檢視器內容,讓使用者在操作網頁時,能感受到更即時、靈活的回應
速度。
接下來,要把先前從伺服器端取回的資料,載入檢視器。載入方式有二:一種做法是利
JavaScript ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

初めてのJavaScript 第3版 ―ES2015以降の最新ウェブ開発

初めてのJavaScript 第3版 ―ES2015以降の最新ウェブ開発

Ethan Brown, 武舎 広幸, 武舎 るみ
PHP编程:第4版

PHP编程:第4版

Kevin Tatroe, Peter MacIntyre
Strange Code

Strange Code

Ronald T. Kneusel
Perfecting Your Thinking Skills

Perfecting Your Thinking Skills

MIT Sloan Management Review

Publisher Resources

ISBN: 9789862766811