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網頁應用程式設計
86
|
第五章
繫結機制(Binding
學會繫結機制,才能真正體會從用戶端產生檢視器的好處。繫結指的是把檢視器元件和
JavaScript 物件(通常是模型)綁在一起。當 JavaScript 物件的內容有變動時,檢視器也
會自動同步更新相關內容。也就是說,把檢視器和模型綁在一起後,只要模型有變動,
就會自動重新產生新的檢視器內容。
繫結可說是非常重要的功能。有了這項功能,就不需要在資料異動時,透過控制器去更
新檢視器內容,一切都會自動完成。此外,善用繫結機制管理應用程式,也可說是為建
立即時應用程式(請參閱本書第八章)舖路。
如果要把 JavaScript 物件和檢視器綁在一起,就得在物件的屬性有異動時,透過一
個回叫函式來觸發檢視器的更新。但 JavaScript 並沒有內建這樣的函式(像 Ruby
Python,就有提供類似
method_missing
的功能),而且目前也沒辦法利用 JavaScript
的取值函式(getter)和設值函式(setter)(
http://ejohn.org/blog/javascript-getters-and-
setters
)模擬出類似的功能。不過由於 JavaScript 是一種非常有彈性的程式語言,因此
還是能自行開發出
change
回叫函式:
var addChange = function(ob){
ob.change = function(callback){
if (callback) {
if ( !this._change ) this._change ...
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