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網頁應用程式設計
204
|
第十二章
// ...
}});
你可以利用
default
這組雜湊物件(hash)來設定所有屬性的預設值。這麼一來,在建
立模型的實體時,所有未特別設定的屬性值,都會被設成預設值:
var Chat = Backbone.Model.extend({
defaults: {
from: "anonymous"
}
});
assertEqual( (new Chat).get("from"), "anonymous" );
集合物件(Collection
Backbone 是把模型實體的陣列,存放在集合物件裡。初學者可能比較難理解,為什麼需
要用集合物件來管理模型,但其實這是蠻常需要用到的手法。舉個簡單的例子,假設你
現在想開發新版的 Twitter,那麼至少會需要兩組集合物件:
Followers
Followees
雖然兩組集合物件裡放的模型物件都是同一種:
User
,但因為兩者需要存的是兩批不同
的使用者,因此會需要用到兩組集合物件來存放。
建立集合物件的做法和建立模型的做法類似,要用
Backbone.Collection
extend()
函式:
var Users = Backbone.Collection.extend({
model: User
});
從前面這段程式碼可以看出,如果想要設定某個集合物件所對應的模型(這段範例是讓
它對應到
User
模型),可以覆寫
model
屬性值。雖然此舉並非必要,但這麼做可以讓集
合物件在必要的時候,知道自己預設對應的模型物件是哪一種。一般來說,集合物件裡 ...
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