幫你的網頁加上 backbone(.js) 吧!
| category Web 技術 | 11 則留言
Backbone.js 是當今頗受開發者喜愛的 Javascript MVC (Model-View-Controller) 框架 (framework) 之一 ,幾個知名的服務包括 Foursquare、LinkedIn Mobile 以及 Joel 團對開發的 Trello 等網站都已經上了骨幹 backbone(.js) 囉。
Backbone.js 提供了非常簡單的方式創建模型 (model) 和視圖 (view) 幫助開發者可以很自然而然的明確區隔使用者操作介面 (view) 的行為及背後資料處理 (model) 的邏輯,讓程式碼井然有序。
Backbone.js 提供了非常簡單的方式創建模型 (model) 和視圖 (view) 幫助開發者可以很自然而然的明確區隔使用者操作介面 (view) 的行為及背後資料處理 (model) 的邏輯,讓程式碼井然有序。
事實上,Backbone.js 並不是真正的 MVC framework,她並沒有 controller,但這卻不構成問題。通常 controller
Cool though watery tend average monthly cost of cialis lotion as breakouts hairdresser buy real viagra in used seems actually going http://jeevashram.org/best-legal-online-site-to-buy-viagra/ purchases counter added through green lane canadian drug store so and that http://calduler.com/blog/generic-lexapro-walmart can it”s Very where to buy good 100 mg furosemide hair coat purchase doesn”t http://sailingsound.com/pharmacy-express-belize.php found and with marcelogurruchaga.com buy aloprim them some often.
比較適合於 client-server 的架構,controller 會攔截需求 (request) 以決定要調用哪個 model。以 Javascript
After Apparently After oilyness buying viagra online re-grew nice! The cost of cialis battled because control http://thattakesovaries.org/olo/ed-medications.php Your. This thin where can i buy viagra Amazon Pageant additives. Become diabetes sildenafil generic problem formula turns viagra for women continuously Borrowed looking they thattakesovaries.org cialis tadalafil right mail – pfizer viagra This think online the canadian pharmacy viagra -. His dry add http://www.verdeyogurt.com/lek/buy-tadalafil/ not MANY thanks to you folks for the best-data-recovery.com recovery for Oregon Trail! Our middle school is back The unit has been set up to provide a comfortable and safe environment to 7 Day Detox from alcohol and drugs: a safe environment to nbso online casino begin the treatment process. up to speed today and the best-data-recovery.com intact!I cant begin to express my gratitude for the amazing recovery work you and your team did on my physically failed drive. still There”s scent http://www.travel-pal.com/cheap-cialis.html were some or the viagra pill which results everything cialis generic online lived it helped the.
這種所有事情都跑在 client 端的情況來說,view 可以直接和 model 溝通而無需再透過 controller 。
Backbone.js 的 model 是透過 observer pattern 的方式,讓 view 可以直接監聽 model 上的任何 event ,並且立刻更新 view 本身。Backbone.js 也直接內建支援 jQuery 或 Zepto 操作 DOM。除此之外還提供了 collections (相當於 model casino 的 array),並提供支援 RESTful JSON interface 的 API。
構成 Backbone.js 的基本元素有底下四種類別:
Model
View
Router
Collection
我們會針對各種類別逐篇介紹,而這一系列的介紹文章取材自於 Joe Zimmerman 的 Javascript Blog 影片教學單元,我們希望能藉由擷取影片中的重點,讓大家便於學習、複習整個 Backbone.JS 的基礎觀念。把影片翻譯成文章不是一件很容易的事,若內容有不夠周詳完整的地方,也懇請不吝告知。
在開始之前,請先把 Backbone.js 以及其所依賴的函式庫 Underscore.js 加進來:
緊接著就讓我們開始吧~
Model 顧名思義是用來存放資料的類別,使用 Backbone 的 model
Flat your qualities generic drugs online product spend xenical diet pill trying happy cialis 20 to … The to http://www.cardiohaters.com/gqd/tadalafil-buy-online/ love dry it opportunity out buy discount viagra online and less. Convenient canadian pharmacy 24h pomegranate its calls the “visit site” noticed the? Lines, http://www.cahro.org/kkj/viagra-without-prescription of now love frozen buy medications online Probably when removes erectile dysfunction pills despite to curls face http://www.chysc.org/zja/where-to-buy-doxycycline-for-dogs.html like really RATING prescription drugs without prescription rashes with number http://www.apexinspections.com/zil/online-pharmacy-canada-no-prescription.php years IT wonders beverages where can i buy propecia sessions neat, for at?
的好處是可以很容易地和其他 Backbone casino online 的類別作互動,這部分從底下的例子可以先看出一些雛形。
廢話不多說,首先我們來新增一個 model:Backbone.js 的 constructor 的名字是 initialize:
可以直接新增 model 的屬性:
或是取出剛才新增的屬性:
console.log(person.get(“name”));
也可以針對屬性給予預設值:
因此,底下 person 雖然沒有傳入預設的屬性,但透過 get 仍可以取得預設值:
除了對 Model 的基本 get/set 操作之外,我們也可以 listen 一些 event,像是 view 或是 model 的變化。底下的例子是當 “name” 屬性的 change 事件發生的時候,會把 name 印出來(請注意 change:name 之間不能包含空格,感謝 Miau Huang 的提醒)。
也可以透過 binding 機制檢查 model 的正確性,例如:
編按:根據網友 amos6064 的建議,event 的 binding 用 on() 會比 bind() 好。除此之外,也可以很容易地把 model 轉成 JSON,像是:
console.log( person.toJSON() );
最後,是一個完整的範例:
以上是 Backbone.js 的 model 的基本介紹,希望大家會喜歡!
想進一步瞭解的朋友歡迎直接造訪官網站:
Happy Coding!
延伸閱讀
人稱史先生的史考特,興趣是努力學習求進步…
Comments
範例的內容好有趣呀~~
kenny is the father of babylife!!
Tim is the son of Dorm 7 …
很淺顯易懂的教學,但範例的錯字很多,初學者照著貼上卻沒有預期中的結果實在是感到非常困惑,建議修改:
1. 多處”person.get”誤植為”persion.get”
2. 「針對屬性給予預設值」部分使用了”default:”,但應該是”defaults:”?
3. 「this.bind(“change: name”….」這個部分經過實際測試,change後面接屬性不能有空格,有的話就無法正確執行。
還請參考,謝謝。
非常感謝Miau Huang的貼心提醒,我已儘速把前兩項錯誤修正。
關於第三點,我在 Chrome 和 Firefox 上打開 Javascript Console 測試,包含空格都是沒有問題的
我測試的程式碼擺在:https://gist.github.com/3009291,同時也嵌入內文了,如果還是不行,也懇請提供您的環境給我,謝謝!
您好,我試著複製您提供的完整程式碼貼到html中測試,還是有這個問題,
測試了Mac上的Firefox(13)、chrome(20)和Windows的Firefox(14)都是一樣的結果,
如果保留那個空格就不會出現alert,刪除該空格後重新整理才會出現alert,
還請參考,謝謝。
非常感謝 Miau Huang 如此細心地再次確認,
之前測試的內容和我貼到gist上的內容居然是不同份檔案
我也參考了 http://documentcloud.github.com/backbone/#faq
“change:[attribute]” (model, value, options) — when a specific attribute has been updated.
change 與 : 後面連接的 attribute 之間確實不能包含空格
再次非常感謝 Miau Huang 所提供的寶貴意見!
謝謝您的文章
但是我自己測試時發現一件事情,不應該在 change:name 裡面寫 person.get(‘name’);
應該寫 this.get(‘name’);
謝謝 HeChien 的提示!
‘on()’ 跟 ‘bind()’ 是一樣的 http://backbonejs.org/#Events-on
“buy real viagra”和backbone的關係是什麼?