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網頁應用程式設計
4
|
第一章
實作 MVC 架構時,其實不一定要用到任何函式庫或框架平台(framework)。關鍵在
於明確定義 MVC 架構中各元件的程式碼區塊,並將元件之間的權責劃分清楚。這麼一
來,就可以獨立開發、測試,以及維護系統中各部分的元件。
接下來再更詳細的說明一下構成 MVC 架構的三大元素。
模型(Model
模型是用來存放所有應用程式資料物件的地方。例如名稱為 User 的模型,就適合用來
存放使用者清單、使用者的相關屬性,以及所有和使用者相關的特定程式邏輯。
模型並不會知道任何和檢視器或控制器相關的資訊。模型唯一該知道的事情,就應該只
有資料本身,以及和資料直接相關的邏輯。至於其他像是事件處理機制、檢視器模版
template),或是和模型無關的邏輯,都應該要和模型劃清界線。如果發現模型裡夾雜
了和檢視器相關的程式碼,就表示這個應用程式已經違反了 MVC 架構的規範。切記,
模型絕對不能和應用程式的其他任何部分,存在耦合關係。
當控制器從伺服器端取得資料或新增資料時,可以將這些資料包進模型的物件實體裡。
這麼一來,就可以運用物件導向的概念來處理這些資料。也就是說,你可以直接呼叫定
義在模型上的任一組函式或邏輯,對資料進行各種處理。
也就是說,像這樣的一段程式:
var user = users["foo"];
destroyUser(user);
可以改寫成這樣:
var user = User.find("foo");
user.destroy();
第一段範例程式碼中,沒有用到命名空間(namespace ...
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