backbone.js1.3.3------------------------view

来源:http://www.cnblogs.com/wangwei1314/archive/2016/06/18/5595887.html
-Advertisement-
Play Games

...


  1 // Backbone.View
  2   // -------------
  3 
  4   // Backbone Views are almost more convention than they are actual code. A View
  5   // is simply a JavaScript object that represents a logical chunk of UI in the
  6   // DOM. This might be a single item, an entire list, a sidebar or panel, or
  7   // even the surrounding frame which wraps your whole app. Defining a chunk of
  8   // UI as a **View** allows you to define your DOM events declaratively, without
  9   // having to worry about render order ... and makes it easy for the view to
 10   // react to specific changes in the state of your models.
 11 
 12   // Creating a Backbone.View creates its initial element outside of the DOM,
 13   // if an existing element is not provided...
 14   //option中可以傳遞的參數包括
 15   //{model:模型名稱,
 16   // collection:集合名稱,
 17   // el:要將html模板添加到的元素,
 18   // id:el的id屬性,若沒有el則預設創建id為這個的div,其屬性為下麵的attributes,tagName,className,
 19   // attributes:
 20   // tagName:
 21   // className:
 22   // events:該元素綁定的事件},
 23   // 在執行自定義的initialize時會將這些屬性,events綁定到元素上
 24   var View = Backbone.View = function(options) {
 25     this.cid = _.uniqueId('view');
 26     this.preinitialize.apply(this, arguments);
 27     _.extend(this, _.pick(options, viewOptions));
 28     this._ensureElement();
 29     this.initialize.apply(this, arguments);
 30   };
 31 
 32   // Cached regex to split keys for `delegate`.
 33   var delegateEventSplitter = /^(\S+)\s*(.*)$/;
 34 
 35   // List of view options to be set as properties.
 36   var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events'];
 37 
 38   // Set up all inheritable **Backbone.View** properties and methods.
 39   _.extend(View.prototype, Events, {
 40 
 41     // The default `tagName` of a View's element is `"div"`.
 42     tagName: 'div',
 43 
 44     // jQuery delegate for element lookup, scoped to DOM elements within the
 45     // current view. This should be preferred to global lookups where possible.
 46     $: function(selector) {
 47       return this.$el.find(selector);
 48     },
 49 
 50     // preinitialize is an empty function by default. You can override it with a function
 51     // or object.  preinitialize will run before any instantiation logic is run in the View
 52     preinitialize: function(){},
 53 
 54     // Initialize is an empty function by default. Override it with your own
 55     // initialization logic.
 56     initialize: function(){},
 57 
 58     // **render** is the core function that your view should override, in order
 59     // to populate its element (`this.el`), with the appropriate HTML. The
 60     // convention is for **render** to always return `this`.
 61     render: function() {
 62       return this;
 63     },
 64 
 65     // Remove this view by taking the element out of the DOM, and removing any
 66     // applicable Backbone.Events listeners.
 67     remove: function() {
 68       this._removeElement();
 69       this.stopListening();
 70       return this;
 71     },
 72 
 73     // Remove this view's element from the document and all event listeners
 74     // attached to it. Exposed for subclasses using an alternative DOM
 75     // manipulation API.
 76     _removeElement: function() {
 77       this.$el.remove();
 78     },
 79 
 80     // Change the view's element (`this.el` property) and re-delegate the
 81     // view's events on the new element.
 82     setElement: function(element) {
 83       this.undelegateEvents();
 84       this._setElement(element);
 85       this.delegateEvents();
 86       return this;
 87     },
 88 
 89     // Creates the `this.el` and `this.$el` references for this view using the
 90     // given `el`. `el` can be a CSS selector or an HTML string, a jQuery
 91     // context or an element. Subclasses can override this to utilize an
 92     // alternative DOM manipulation API and are only required to set the
 93     // `this.el` property.
 94     _setElement: function(el) {
 95       this.$el = el instanceof Backbone.$ ? el : Backbone.$(el);
 96       this.el = this.$el[0];
 97     },
 98 
 99     // Set callbacks, where `this.events` is a hash of
100     //
101     // *{"event selector": "callback"}*
102     //
103     //     {
104     //       'mousedown .title':  'edit',
105     //       'click .button':     'save',
106     //       'click .open':       function(e) { ... }
107     //     }
108     //
109     // pairs. Callbacks will be bound to the view, with `this` set properly.
110     // Uses event delegation for efficiency.
111     // Omitting the selector binds the event to `this.el`.
112     delegateEvents: function(events) {
113       events || (events = _.result(this, 'events'));
114       if (!events) return this;
115       this.undelegateEvents();
116       for (var key in events) {
117         var method = events[key];
118         if (!_.isFunction(method)) method = this[method];
119         if (!method) continue;
120         var match = key.match(delegateEventSplitter);
121         this.delegate(match[1], match[2], _.bind(method, this));
122       }
123       return this;
124     },
125 
126     // Add a single event listener to the view's element (or a child element
127     // using `selector`). This only works for delegate-able events: not `focus`,
128     // `blur`, and not `change`, `submit`, and `reset` in Internet Explorer.
129     delegate: function(eventName, selector, listener) {
130       this.$el.on(eventName + '.delegateEvents' + this.cid, selector, listener);
131       return this;
132     },
133 
134     // Clears all callbacks previously bound to the view by `delegateEvents`.
135     // You usually don't need to use this, but may wish to if you have multiple
136     // Backbone views attached to the same DOM element.
137     undelegateEvents: function() {
138       if (this.$el) this.$el.off('.delegateEvents' + this.cid);
139       return this;
140     },
141 
142     // A finer-grained `undelegateEvents` for removing a single delegated event.
143     // `selector` and `listener` are both optional.
144     undelegate: function(eventName, selector, listener) {
145       this.$el.off(eventName + '.delegateEvents' + this.cid, selector, listener);
146       return this;
147     },
148 
149     // Produces a DOM element to be assigned to your view. Exposed for
150     // subclasses using an alternative DOM manipulation API.
151     _createElement: function(tagName) {
152       return document.createElement(tagName);
153     },
154 
155     // Ensure that the View has a DOM element to render into.
156     // If `this.el` is a string, pass it through `$()`, take the first
157     // matching element, and re-assign it to `el`. Otherwise, create
158     // an element from the `id`, `className` and `tagName` properties.
159     _ensureElement: function() {
160       if (!this.el) {
161         var attrs = _.extend({}, _.result(this, 'attributes'));
162         if (this.id) attrs.id = _.result(this, 'id');
163         if (this.className) attrs['class'] = _.result(this, 'className');
164         this.setElement(this._createElement(_.result(this, 'tagName')));
165         this._setAttributes(attrs);
166       } else {
167         this.setElement(_.result(this, 'el'));
168       }
169     },
170 
171     // Set attributes from a hash on this view's element.  Exposed for
172     // subclasses using an alternative DOM manipulation API.
173     _setAttributes: function(attributes) {
174       this.$el.attr(attributes);
175     }
176 
177   });

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 先簡要概述一下video標簽: video:嵌入視頻到頁面中 1. 聲明video標簽 單個視頻的時候使用src: Your browser does not support the video element. 多個視頻的時候使用標簽: Your browser does not support ... ...
  • backbone的router和history對象就是對window.history對象的操作。 學習backbone的router和history之前必須要學習window.history對象。html5給開發者添加了操作history的api。 這裡需要瞭解兩個概念: hash:個人理解,has ...
  • 效果展示 http://hovertree.com/texiao/nav/4/手機掃描二維碼查看效果:源碼下載 http://hovertree.com/h/bjaf/kroft6c7.htm效果圖如下:代碼如下: <!doctype html> <html lang="zh"> <head> <m ...
  • 隨機色有兩種格式: 效果預覽:http://wjf444128852.github.io/DEMOLIST/JS/test/index.html 1、rgb(xxx,xxx,xxx) 2、#xxxxxx 下麵實現兩種隨機的方法 思路: 就是如何讓x都是隨機的, 1、中的xxx是0-255之間的隨機整 ...
  • 總結:總的來說,這些控制項可以在官網找到列子, 部分ui效果不如意的,可根據jQueryUI上添加的類選擇器等,進行再加工 ...
  • 最近CTO給我分配了一個移動端H5開發的任務,主要功能是需要實現翻書效果,我聽過主要需求後,當時是呀!!!接下來自己嘗試使用 fullPage.js和Swiper來實現翻書效果,結果效果都不是非常的理想,後來想起自己曾經做過PC版的翻書效果,當時使用的是Turn.js ,查過其相關API後,整個人突 ...
  • 前言:1.使用setInterval()的定時器會把事件運行的時間也包含在內,如果要精確算定時兩個任務之間的時間,可以使用setTimeout()替換。2.當非同步事件發生時,如mouse click, a timer firing, or an XMLHttpRequest completing(鼠 ...
  • 對象,是javascript中非常重要的一個梗,是否能透徹的理解它直接關係到你對整個javascript體系的基礎理解,說白了,javascript就是一群對象在攪。。(嗶!)。 常用的幾種對象創建模式 使用new關鍵字創建 最基礎的對象創建方式,無非就是和其他多數語言一樣說的一樣:沒對象,你new ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...