SlideShare a Scribd company logo
1 of 14
Backbone.js and RequireJS

為何需要 Backbone 與 requireJS
●




RequireJS
●




Project 檔案架構
●




自訂 Backbone Class
●




Router
●




Model
●




Collection
●




View
●
一 . 為何需要 BackboneJS 與 RequireJS

●
    使用 Backbone 將程式結構化
    將一堆的 js function, 依照 MVC 架構切割成一個個的 Class 物件
●
    使用 RequireJS 將物件模組化
    將產生的 Class 物件包裝成一個個獨立檔案的 Module ,去除物件與物件
    間的直接關連性
●
    減少 Global 變數存在
●
    使用 RequireJS 做檔案最佳化
    使用 Optimizer, 將所有檔案壓縮並 compile 成單一檔案
二 .RequireJS

●
    使用
    <script data-main="main" src="require.js"></script>
●
    程式進入點
●
    1. 設定 require.config()
       baseUrl : 設定所有 js 找檔案的 root 節點,
                  預設是 main.js 所在路徑
       paths : 對檔案或目錄,設定別名在 module 中使用
       urlArgs : 可在 runtime 時替所有 js 都加入 string ,
                  避免 JS Cache
●
    2. 執行 app 邏輯 require([],function(){})
    把在 project 內需要共同監聽的物件在這裡產生 instance
    並設定到 App 內讓需要的物件可以參考
二 .RequireJS

●
    Module 定義
    1.
    define(['_Underscore','_backbone','_$'],function(_,Backbone,$){
         return {
         }
    })


    2.
    define(function(require){
         var $ = require('_$'),
         _ = require('_Underscore');
         return {
         }
    })
二 .RequireJS

●
    Module 應用模式
    1.singleton instance
       定義 App.js
       define([],function(){
            return {....}
       })
       使用 App.initialize();


    2.Use Class
       定義 MyView.js
       define(['_backbone'],function(Backbone){
            return Backbone.View.extend({})
       })
       使用 var myView=new MyView();
三 .Project 檔案架構

●
    main.js : project 的進入點
●
    app.js : 初始化 App 產生所有要用 Module 的 instance
●
    router.js : 使用者 url 經過 router 處理 , 呼叫對應要執行的 function
●
    text.js 與 order.js 是 RequireJS 的 Plugin, 若有用到則放在這裡
●
    views/,collections/,models/ : Backbone 產生的各個物件 , 包成
    Module 後 , 分別放入各個目錄內
●
    templates/ : 要提供給 view render 使用的 html 樣板檔案
●
    libs/ : 會用到的各種外部 resource lib 放在這裡,如 jQuery..
四 . 自訂 Backbone Class

●
    自訂 Class
    framework 內已經定義好所有的基礎 Class, 依照需要 extend 相關 Class
    即可
    例
        MyRouter=Backbone.Router.extend({..})
        MyModel==Backbone.Model.extend({..})
        MyView==Backbone.View.extend({..})
●
    initialize
    initialize 會在 new instance 時被自動執行
    new instance 要傳進去的 option 設定都會被傳到這處理
●
    使用 _.bindAll
    將 module 內設定的 function bind 到 Module 上使用 this 來呼叫才會有效
五 .Router

    在 routes 設定規則,以 URL 決定要呼叫執行的 functon

●
    設定 "/login" :"loginPage"
    當網址是 localhost/myproject/#/login
    會執行 router 裡面的 loginPage()

●
    設定 "/prod/:id" :"prodPage"
    當網址是 localhost/myproject/#/prod/3
    會執行 prodPage(id), 並得到一個 id=3 的參數
六 .Model

●
    當作一般資料儲存物件
    var model=new Backbone.Model({
        x:123,
        y:456
    })
●
    可自行與 Server 溝通取得資料
    以 model.fetch() 取得資料 , 資料會儲存在 model 內 , 再透過 model 提供
    的 function 操作
    var Item=Backbone.Model.extend({
         url:function(){
               return "xxx.php?id="+this.get("id");
          }
    });
    var item=new Item();
    item.fetch()
六 .Model

●
    可被監聽
    利用 model.set() 儲存變數值,則可針對這變數做監聽
    var myModel=new Backbone.Model({
        x:123,
        y:456
    })

    var view=new Backbone.View({
         model:myModel,
         initialize:function(){
               model.on('change:x',this.onChangeHandler)
         },
         onChangeHandler:function(){
         }
    })
六 .Model

●
    使用模式
    當做 Collection 取得大批資料後儲存的資料格式
    取代 global 變數,將 model 當做共用資料物件 , 監聽 model 內值變化
●
    重要性質
    id:
    cid:系統自動產生的唯一値
    attributes: 在這裡的資料才可被監聽
    url: 處理產生要與 server 取得資料的 url 路徑
    parse: 可改寫這部分,對接收到值做加工處理
    sync: 改寫這部分,可以讓 CRUD 使用不同 URL
七 .Collection

用來取得批次資料 , 儲存資料在 collection 內大多數屬性與
model 相同

1.collection.get(id) 取得指定 id 的 model 物件

2.at(index) 取得指定 index 的 model 物件

3.length 取得 collection 內 model 的數量

4.collection.models:array 直接取得 models array
八 .View

    用來產生頁面元件 , 並直接接收與使用者互動的 event
●
    產生 instance
    options 資料到帶到 initialize 內,部分特定屬性同時也會寫到 view 的屬性
    上
    如 model, collection, el, id, className, and tagName
●
    重要性質
    el: 產生 view instance 時 , 設定要指到實體的 dom 物件 , 值的設定是
    selector 字串
       var homePageHeader = new HomePageHeaderView({
                  el: "#homeHeader",
       })
    $el:jQuery 包裝好的 el 元素 , 可以 jQuery function 操作
         this.$el.empty()
    $: view.$(selector) 可以使用 jQuery 在 view 內部操作
         this.$(".title").text()
    render: 預設會在這裡產生 html, 依照需求改寫
八 .View

    使用 Underscore 的 Template
●
    載入樣板檔案 透過 requireJS 的 text plugin 載入
●
    使用 underscore template
      var data = {
                items: models,
                _: _
      };
      var _html = _.template(tmplFile, data);
      this.$el.append(_html);

More Related Content

What's hot

JQuery 学习
JQuery 学习JQuery 学习
JQuery 学习cssrain
 
16 CoreData
16 CoreData16 CoreData
16 CoreDataTom Fan
 
02 Objective-C
02 Objective-C02 Objective-C
02 Objective-CTom Fan
 
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Justin Lin
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCellTom Fan
 
Sina App Quick Guide 1
Sina App Quick Guide 1Sina App Quick Guide 1
Sina App Quick Guide 1guestf4aed35
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101Jollen Chen
 
Node getting-started
Node getting-startedNode getting-started
Node getting-startedlylijincheng
 
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAPiOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAPMing-Sian Lin
 
05 MapKit and Text Input
05 MapKit and Text Input05 MapKit and Text Input
05 MapKit and Text InputTom Fan
 
Underscore
UnderscoreUnderscore
Underscorecazhfe
 
Ch09 整合資料庫
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫Justin Lin
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換Justin Lin
 
jQuery 選取器解析
jQuery 選取器解析jQuery 選取器解析
jQuery 選取器解析Kingsley Zheng
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application StatesTom Fan
 
CH09:Collection與Map
CH09:Collection與MapCH09:Collection與Map
CH09:Collection與MapJustin Lin
 

What's hot (20)

Phalcon框架入门
Phalcon框架入门Phalcon框架入门
Phalcon框架入门
 
JQuery 学习
JQuery 学习JQuery 学习
JQuery 学习
 
16 CoreData
16 CoreData16 CoreData
16 CoreData
 
Mvc
MvcMvc
Mvc
 
02 Objective-C
02 Objective-C02 Objective-C
02 Objective-C
 
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCell
 
Sina App Quick Guide 1
Sina App Quick Guide 1Sina App Quick Guide 1
Sina App Quick Guide 1
 
I os 08
I os 08I os 08
I os 08
 
Node way
Node wayNode way
Node way
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101
 
Node getting-started
Node getting-startedNode getting-started
Node getting-started
 
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAPiOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
 
05 MapKit and Text Input
05 MapKit and Text Input05 MapKit and Text Input
05 MapKit and Text Input
 
Underscore
UnderscoreUnderscore
Underscore
 
Ch09 整合資料庫
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換
 
jQuery 選取器解析
jQuery 選取器解析jQuery 選取器解析
jQuery 選取器解析
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application States
 
CH09:Collection與Map
CH09:Collection與MapCH09:Collection與Map
CH09:Collection與Map
 

Viewers also liked

2012 review
2012 review2012 review
2012 review106174CL
 
Dulcelandia Febrero 2010
Dulcelandia Febrero 2010Dulcelandia Febrero 2010
Dulcelandia Febrero 2010ademirac
 
UI Engineering Introduction
UI Engineering IntroductionUI Engineering Introduction
UI Engineering IntroductionHsuan Fu Lien
 
前端框架Redux實作
前端框架Redux實作前端框架Redux實作
前端框架Redux實作Chi-wen Sun
 
前端框架發展
 前端框架發展 前端框架發展
前端框架發展Chi-wen Sun
 
estratto_maestri_nella_nuova_energia
estratto_maestri_nella_nuova_energiaestratto_maestri_nella_nuova_energia
estratto_maestri_nella_nuova_energiaLaviadelcuore
 
Nodejs api server_implement
Nodejs api server_implementNodejs api server_implement
Nodejs api server_implementChi-wen Sun
 
Overview of Backbone
Overview of BackboneOverview of Backbone
Overview of BackboneJohn Ashmead
 
Modern web develop
Modern web developModern web develop
Modern web developChi-wen Sun
 
Purple people eater
Purple people eaterPurple people eater
Purple people eaternanwood
 
前端MVC之backbone
前端MVC之backbone前端MVC之backbone
前端MVC之backboneJerry Xie
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your BusinessBarry Feldman
 

Viewers also liked (13)

2012 review
2012 review2012 review
2012 review
 
Dulcelandia Febrero 2010
Dulcelandia Febrero 2010Dulcelandia Febrero 2010
Dulcelandia Febrero 2010
 
UI Engineering Introduction
UI Engineering IntroductionUI Engineering Introduction
UI Engineering Introduction
 
ხათუნა
ხათუნახათუნა
ხათუნა
 
前端框架Redux實作
前端框架Redux實作前端框架Redux實作
前端框架Redux實作
 
前端框架發展
 前端框架發展 前端框架發展
前端框架發展
 
estratto_maestri_nella_nuova_energia
estratto_maestri_nella_nuova_energiaestratto_maestri_nella_nuova_energia
estratto_maestri_nella_nuova_energia
 
Nodejs api server_implement
Nodejs api server_implementNodejs api server_implement
Nodejs api server_implement
 
Overview of Backbone
Overview of BackboneOverview of Backbone
Overview of Backbone
 
Modern web develop
Modern web developModern web develop
Modern web develop
 
Purple people eater
Purple people eaterPurple people eater
Purple people eater
 
前端MVC之backbone
前端MVC之backbone前端MVC之backbone
前端MVC之backbone
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
 

Similar to Backbone js and requirejs

Django development
Django developmentDjango development
Django developmentloveyudu
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniterChun-Kai Wang
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现hua qiu
 
Uliweb设计分享
Uliweb设计分享Uliweb设计分享
Uliweb设计分享modou li
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非Tony Deng
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文banq jdon
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xBo-Yi Wu
 
Kissy模块化实践
Kissy模块化实践Kissy模块化实践
Kissy模块化实践yiming he
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架fangdeng
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架wtxidian
 
Kindeditor设计思路v2
Kindeditor设计思路v2Kindeditor设计思路v2
Kindeditor设计思路v2luolonghao
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文Guo Albert
 
Chapter 4 models
Chapter 4 modelsChapter 4 models
Chapter 4 modelsEkman Hsieh
 
Nginx使用和模块开发
Nginx使用和模块开发Nginx使用和模块开发
Nginx使用和模块开发qingpiao1983
 
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)Will Huang
 
2023-netconf-deploy-azure-function-with-KEDA-on-aks
2023-netconf-deploy-azure-function-with-KEDA-on-aks2023-netconf-deploy-azure-function-with-KEDA-on-aks
2023-netconf-deploy-azure-function-with-KEDA-on-aksRoberson Liou
 
掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001rainx1982
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發koji lin
 

Similar to Backbone js and requirejs (20)

Django development
Django developmentDjango development
Django development
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniter
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
 
Uliweb设计分享
Uliweb设计分享Uliweb设计分享
Uliweb设计分享
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
CRUD 綜合運用
CRUD 綜合運用CRUD 綜合運用
CRUD 綜合運用
 
Kissy模块化实践
Kissy模块化实践Kissy模块化实践
Kissy模块化实践
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架
 
Kindeditor设计思路v2
Kindeditor设计思路v2Kindeditor设计思路v2
Kindeditor设计思路v2
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
 
Chapter 4 models
Chapter 4 modelsChapter 4 models
Chapter 4 models
 
Nginx使用和模块开发
Nginx使用和模块开发Nginx使用和模块开发
Nginx使用和模块开发
 
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
 
2023-netconf-deploy-azure-function-with-KEDA-on-aks
2023-netconf-deploy-azure-function-with-KEDA-on-aks2023-netconf-deploy-azure-function-with-KEDA-on-aks
2023-netconf-deploy-azure-function-with-KEDA-on-aks
 
掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發
 

Backbone js and requirejs

  • 1. Backbone.js and RequireJS 為何需要 Backbone 與 requireJS ● RequireJS ● Project 檔案架構 ● 自訂 Backbone Class ● Router ● Model ● Collection ● View ●
  • 2. 一 . 為何需要 BackboneJS 與 RequireJS ● 使用 Backbone 將程式結構化 將一堆的 js function, 依照 MVC 架構切割成一個個的 Class 物件 ● 使用 RequireJS 將物件模組化 將產生的 Class 物件包裝成一個個獨立檔案的 Module ,去除物件與物件 間的直接關連性 ● 減少 Global 變數存在 ● 使用 RequireJS 做檔案最佳化 使用 Optimizer, 將所有檔案壓縮並 compile 成單一檔案
  • 3. 二 .RequireJS ● 使用 <script data-main="main" src="require.js"></script> ● 程式進入點 ● 1. 設定 require.config() baseUrl : 設定所有 js 找檔案的 root 節點, 預設是 main.js 所在路徑 paths : 對檔案或目錄,設定別名在 module 中使用 urlArgs : 可在 runtime 時替所有 js 都加入 string , 避免 JS Cache ● 2. 執行 app 邏輯 require([],function(){}) 把在 project 內需要共同監聽的物件在這裡產生 instance 並設定到 App 內讓需要的物件可以參考
  • 4. 二 .RequireJS ● Module 定義 1. define(['_Underscore','_backbone','_$'],function(_,Backbone,$){ return { } }) 2. define(function(require){ var $ = require('_$'), _ = require('_Underscore'); return { } })
  • 5. 二 .RequireJS ● Module 應用模式 1.singleton instance 定義 App.js define([],function(){ return {....} }) 使用 App.initialize(); 2.Use Class 定義 MyView.js define(['_backbone'],function(Backbone){ return Backbone.View.extend({}) }) 使用 var myView=new MyView();
  • 6. 三 .Project 檔案架構 ● main.js : project 的進入點 ● app.js : 初始化 App 產生所有要用 Module 的 instance ● router.js : 使用者 url 經過 router 處理 , 呼叫對應要執行的 function ● text.js 與 order.js 是 RequireJS 的 Plugin, 若有用到則放在這裡 ● views/,collections/,models/ : Backbone 產生的各個物件 , 包成 Module 後 , 分別放入各個目錄內 ● templates/ : 要提供給 view render 使用的 html 樣板檔案 ● libs/ : 會用到的各種外部 resource lib 放在這裡,如 jQuery..
  • 7. 四 . 自訂 Backbone Class ● 自訂 Class framework 內已經定義好所有的基礎 Class, 依照需要 extend 相關 Class 即可 例 MyRouter=Backbone.Router.extend({..}) MyModel==Backbone.Model.extend({..}) MyView==Backbone.View.extend({..}) ● initialize initialize 會在 new instance 時被自動執行 new instance 要傳進去的 option 設定都會被傳到這處理 ● 使用 _.bindAll 將 module 內設定的 function bind 到 Module 上使用 this 來呼叫才會有效
  • 8. 五 .Router 在 routes 設定規則,以 URL 決定要呼叫執行的 functon ● 設定 "/login" :"loginPage" 當網址是 localhost/myproject/#/login 會執行 router 裡面的 loginPage() ● 設定 "/prod/:id" :"prodPage" 當網址是 localhost/myproject/#/prod/3 會執行 prodPage(id), 並得到一個 id=3 的參數
  • 9. 六 .Model ● 當作一般資料儲存物件 var model=new Backbone.Model({ x:123, y:456 }) ● 可自行與 Server 溝通取得資料 以 model.fetch() 取得資料 , 資料會儲存在 model 內 , 再透過 model 提供 的 function 操作 var Item=Backbone.Model.extend({ url:function(){ return "xxx.php?id="+this.get("id"); } }); var item=new Item(); item.fetch()
  • 10. 六 .Model ● 可被監聽 利用 model.set() 儲存變數值,則可針對這變數做監聽 var myModel=new Backbone.Model({ x:123, y:456 }) var view=new Backbone.View({ model:myModel, initialize:function(){ model.on('change:x',this.onChangeHandler) }, onChangeHandler:function(){ } })
  • 11. 六 .Model ● 使用模式 當做 Collection 取得大批資料後儲存的資料格式 取代 global 變數,將 model 當做共用資料物件 , 監聽 model 內值變化 ● 重要性質 id: cid:系統自動產生的唯一値 attributes: 在這裡的資料才可被監聽 url: 處理產生要與 server 取得資料的 url 路徑 parse: 可改寫這部分,對接收到值做加工處理 sync: 改寫這部分,可以讓 CRUD 使用不同 URL
  • 12. 七 .Collection 用來取得批次資料 , 儲存資料在 collection 內大多數屬性與 model 相同 1.collection.get(id) 取得指定 id 的 model 物件 2.at(index) 取得指定 index 的 model 物件 3.length 取得 collection 內 model 的數量 4.collection.models:array 直接取得 models array
  • 13. 八 .View 用來產生頁面元件 , 並直接接收與使用者互動的 event ● 產生 instance options 資料到帶到 initialize 內,部分特定屬性同時也會寫到 view 的屬性 上 如 model, collection, el, id, className, and tagName ● 重要性質 el: 產生 view instance 時 , 設定要指到實體的 dom 物件 , 值的設定是 selector 字串 var homePageHeader = new HomePageHeaderView({ el: "#homeHeader", }) $el:jQuery 包裝好的 el 元素 , 可以 jQuery function 操作 this.$el.empty() $: view.$(selector) 可以使用 jQuery 在 view 內部操作 this.$(".title").text() render: 預設會在這裡產生 html, 依照需求改寫
  • 14. 八 .View 使用 Underscore 的 Template ● 載入樣板檔案 透過 requireJS 的 text plugin 載入 ● 使用 underscore template var data = { items: models, _: _ }; var _html = _.template(tmplFile, data); this.$el.append(_html);