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網頁應用程式設計
第四章
控制器(Controller)和狀態
過去一直以來,管理狀態的工作,都是由伺服器端的 session cookies 負責。因此使用者
只要一切換至新網頁,前一頁的狀態就沒了(只有用戶端的 cookies 得以保存)。不過
JavaScript 應用程式能把所有的動作都放在同一張網頁上進行,這也意味著:現在我們
能把狀態存在用戶端的記憶體裡。
將狀態存在用戶端最主要的好處,就是能讓系統操作介面的回應更為敏捷。使用者在操
作系統時,不需要花上好幾秒鐘等待載入下一張網頁,就能馬上看到結果。回應速度快
的系統,能大幅改善使用者的操作體驗,這點也是許多 JavaScript 應用程式讓人愛不釋
手的主要原因。
不過話說回來,要將狀態儲存在用戶端,的確是項不小的挑戰。究竟該把狀態存在哪裡
呢?區域變數裡?還是 DOM 元件裡?大部分的程式設計師會在這裡發生失誤,最後往
往導致一些嚴重的問題。畢竟,正確的儲存狀態,可說是開發網頁應用程式最關鍵的環
節之一。
首先要記住,應該儘量避免將狀態存在 DOM 元件裡。這麼做很容易為日後帶來無窮無
盡的麻煩!本書介紹的做法(別忘了,我們用的是歷經眾人反覆測試、驗證過的 MVC
架構),是將狀態儲存在應用程式的控制器(controller)裡面。
控制器究竟是什麼呢?你可以把它想成是檢視器和模型之間的接著劑。它會是整個應用
程式中,唯一認識檢視器和模型,並會幫兩者建立關聯的元件。控制器會在載入網頁
時,幫檢視器設定一些事件處理器、實作事件處理器的回叫函式(callback),並會視情 ...
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