SlideShare a Scribd company logo
1 of 12
Backbone JS 介紹
2013/10/04
Bryan Lin
Agenda
 Introduction
 Model
 View
Introduction
 Javascript MVC (Model-View-Controller) 框架
 提供了非常簡單的方式創建模型 (model) 和視圖 (view) 幫助開發者可以很自然而
然的明確區隔使用者操作介面 (view) 的行為及背後資料處理 (model) 的邏輯
 Backbone.js 的 model 是透過 observer pattern 的方式,讓 view 可以直接監聽
model 上的任何 event ,並且立刻更新 view 本身
 支援 jQuery 或 Zepto 操作 DOM
 提供支援 RESTful JSON interface 的 API
Model
 Model 顧名思義是用來存放資料的類別,使用 Backbone 的 model 的好處是可以
很容易地和其他 Backbone 的類別作互動。
 Backbone.js 的 constructor 的名字是 initialize
Person = Backbone.Model.extend({
initialize: function() {
console.log('hello world!');
}
});
Model
 可以直接新增 model 的屬性:
var person = new Person({ name: "Joe", height: "6 feet" });
Model
 取出剛才新增的屬性:
person.get(‘name’)
Model
 針對屬性給予預設值:
Person = Backbone.Model.extend({
initialize: function() {
console.log("hello world!");
},
defaults: {
name: "Scott",
height: "unknown"
}
});
Model
 綁定change 事件(change:name中間不可包含空格)
Person = Backbone.Model.extend({
initialize: function() {
console.log("hello world!");
this.bind("change:name", function() {
console.log(person.get('name'));
});
}
//...
});
Model
 檢查 model 的正確性
Person = Backbone.Model.extend({
initialize: function() {
console.log('hello world!');
this.bind("change:name", function() {
console.log(persion.get('name'));
});
this.bind("error", function(model, error) {
console.log('error');
});
},
validate: function( attributes ) {
if (attributes.name == 'kenny' ) {
return 'Kenny is the founder of babylife';
}
},
});
View
 initialize :View 的建構子
 el :View 對應到的 DOM 物件
 event:註冊事件
 template 是指繪製 View 的時候可套用的template
 render: 操作改變 el 的外觀
View
var myView = Backbone.View.extend({
initialize: function () {
console.log('create view');
},
el: '#container',
events: {
"click": "render"
},
template: $('#list-template').children(),
render: function() {
var data = this.model.get('data');
for (var i=0, l=data.length; i<l; i++) {
var a = this.template.clone().find('a');
var li = a.attr('href', data[i].href)
li = li.text(data[i].text).end();
this.$el.find('ul').append(li);
}
}
});
範例程式
 http://jsfiddle.net/rightson/xp4pD/

More Related Content

Similar to 20131004 - Backbone js 介紹 by Bryan

程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號鍾誠 陳鍾誠
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC
 
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4twMVC
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文banq jdon
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing江華 奚
 
Uliweb设计分享
Uliweb设计分享Uliweb设计分享
Uliweb设计分享modou li
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
Js高级技巧
Js高级技巧Js高级技巧
Js高级技巧fool2fish
 
Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4Kyle Shen
 
Angular js入门分享 by 王栋
Angular js入门分享   by 王栋Angular js入门分享   by 王栋
Angular js入门分享 by 王栋栋 王
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现yiditushe
 
Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2twMVC
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC
 
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日twMVC
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Jollen Chen
 
Jswebapps
JswebappsJswebapps
Jswebappsjay li
 
Asp.net mvc 基礎
Asp.net mvc 基礎Asp.net mvc 基礎
Asp.net mvc 基礎Gelis Wu
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejsChi-wen Sun
 

Similar to 20131004 - Backbone js 介紹 by Bryan (20)

程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
 
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
 
Uliweb设计分享
Uliweb设计分享Uliweb设计分享
Uliweb设计分享
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
Js高级技巧
Js高级技巧Js高级技巧
Js高级技巧
 
Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4
 
Angular js入门分享 by 王栋
Angular js入门分享   by 王栋Angular js入门分享   by 王栋
Angular js入门分享 by 王栋
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现
 
Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有
 
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101
 
Jswebapps
JswebappsJswebapps
Jswebapps
 
Asp.net mvc 基礎
Asp.net mvc 基礎Asp.net mvc 基礎
Asp.net mvc 基礎
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejs
 

More from LearningTech

More from LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

20131004 - Backbone js 介紹 by Bryan

  • 3. Introduction  Javascript MVC (Model-View-Controller) 框架  提供了非常簡單的方式創建模型 (model) 和視圖 (view) 幫助開發者可以很自然而 然的明確區隔使用者操作介面 (view) 的行為及背後資料處理 (model) 的邏輯  Backbone.js 的 model 是透過 observer pattern 的方式,讓 view 可以直接監聽 model 上的任何 event ,並且立刻更新 view 本身  支援 jQuery 或 Zepto 操作 DOM  提供支援 RESTful JSON interface 的 API
  • 4. Model  Model 顧名思義是用來存放資料的類別,使用 Backbone 的 model 的好處是可以 很容易地和其他 Backbone 的類別作互動。  Backbone.js 的 constructor 的名字是 initialize Person = Backbone.Model.extend({ initialize: function() { console.log('hello world!'); } });
  • 5. Model  可以直接新增 model 的屬性: var person = new Person({ name: "Joe", height: "6 feet" });
  • 7. Model  針對屬性給予預設值: Person = Backbone.Model.extend({ initialize: function() { console.log("hello world!"); }, defaults: { name: "Scott", height: "unknown" } });
  • 8. Model  綁定change 事件(change:name中間不可包含空格) Person = Backbone.Model.extend({ initialize: function() { console.log("hello world!"); this.bind("change:name", function() { console.log(person.get('name')); }); } //... });
  • 9. Model  檢查 model 的正確性 Person = Backbone.Model.extend({ initialize: function() { console.log('hello world!'); this.bind("change:name", function() { console.log(persion.get('name')); }); this.bind("error", function(model, error) { console.log('error'); }); }, validate: function( attributes ) { if (attributes.name == 'kenny' ) { return 'Kenny is the founder of babylife'; } }, });
  • 10. View  initialize :View 的建構子  el :View 對應到的 DOM 物件  event:註冊事件  template 是指繪製 View 的時候可套用的template  render: 操作改變 el 的外觀
  • 11. View var myView = Backbone.View.extend({ initialize: function () { console.log('create view'); }, el: '#container', events: { "click": "render" }, template: $('#list-template').children(), render: function() { var data = this.model.get('data'); for (var i=0, l=data.length; i<l; i++) { var a = this.template.clone().find('a'); var li = a.attr('href', data[i].href) li = li.text(data[i].text).end(); this.$el.find('ul').append(li); } } });