Backbone是一种Web端的MVC框架,这里纪录学习Model,View和Collection的笔记。

1 View

initialize构造函数

Backbone.View 与jQuery库紧密结合。

var SearchView = Backbone.View.extend({

  initialize:function(){ alert("Alerts suck.");

} });

var search_view = new SearchView();

initialize 就类似构造函数。

"el" 属性

"el" 属性关联 the DOM object. 每个Backbone的View都有一个 "el" 属性, 如果未指定,Backbone会创建一个空得el,指向一个空得div元素。

载入template

Backbone的这项功能依赖Underscore库。Underscore提供了一个函数集,算是jQuery功能的补充,函数集可以在这里查询:

http://underscorejs.org/

而Template是Underscore中提供的一个函数,其主要作用是将JS中可能遇到的HTML的代码剥离出来,以文件的形式加载。这样提高了JS代码的可控性,HTML代码也更容易编辑了。让我们看一个例子:

<script type="text/template" id="search_template">
<label>Search</label>
<input type="text" id="search_input" />
<input type="button" id="search_button" value="Search" />
</script> <div id="search_container"></div> <script type="text/javascript">
var SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
// Compile the template using underscore
var template = _.template( $("#search_template").html(), {} );
// Load the compiled HTML into the Backbone "el"
this.$el.html( template );
}
}); var search_view = new SearchView({ el: $("#search_container") });
</script>
search_template中的界面布局部分会被template加载进来用于最后的显示。但如何将search_template的部分单独做为一个文件存放,还没弄明白。

添加事件
events: {
"click input[type=button]": "doSearch"
},
doSearch: function( event ){
// Button clicked, you can access the element that was clicked with event.currentTarget
alert( "Search for " + $("#search_input").val() );
}

添加变量

<label><%= search_label %></label> 这句是添加变量的写法。

<script type="text/template" id="search_template">
<!-- Access template variables with <%= %> -->
<label><%= search_label %></label>
<input type="text" id="search_input" />
<input type="button" id="search_button" value="Search" />
</script>

添加变量的功能可以动态指定界面中显示变量的值。

var SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
var variables = { search_label: "My Search" };
var template = _.template( $("#search_template").html(), variables );
this.$el.html( template );
},
events: {
"click input[type=button]": "doSearch"
},
doSearch: function( event ){
// Button clicked, you can access the element that was clicked with event.currentTarget
alert( "Search for " + $("#search_input").val() );
}
}); var search_view = new SearchView({ el: $("#search_container") });

参考资料:

https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-view

 

Backbone学习笔记 - View篇的更多相关文章

  1. Backbone学习笔记 - Model篇

    2 Model 在Backbone中,Model用于存储核心数据,可以将数据交互相关的逻辑代码放在这里.基本形式如下: var Human = Backbone.Model.extend({ init ...

  2. Backbone学习笔记一Backbone中的MVC

    原文章地址http://bigdots.github.io/2015/12/01/Backbone学习笔记(一)/#more Backbone.js为复杂WEB应用程序提供模型(models).集合( ...

  3. PHP学习笔记 - 进阶篇(11)

    PHP学习笔记 - 进阶篇(11) 数据库操作 PHP支持哪些数据库 PHP通过安装相应的扩展来实现数据库操作,现代应用程序的设计离不开数据库的应用,当前主流的数据库有MsSQL,MySQL,Syba ...

  4. PHP学习笔记 - 进阶篇(10)

    PHP学习笔记 - 进阶篇(10) 异常处理 抛出一个异常 从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过throw抛出,异常抛出之后,后面的代码将不会再被 ...

  5. PHP学习笔记 - 进阶篇(9)

    PHP学习笔记 - 进阶篇(9) 图形图像操作 GD库简介 GD指的是Graphic Device,PHP的GD库是用来处理图形的扩展库,通过GD库提供的一系列API,可以对图像进行处理或者直接生成新 ...

  6. PHP学习笔记 - 进阶篇(8)

    PHP学习笔记 - 进阶篇(8) 日期与时间 取得当前的Unix时间戳 UNIX 时间戳(英文叫做:timestamp)是 PHP 中关于时间与日期的一个很重要的概念,它表示从 1970年1月1日 0 ...

  7. PHP学习笔记 - 进阶篇(7)

    PHP学习笔记 - 进阶篇(7) 文件操作 读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $conte ...

  8. PHP学习笔记 - 进阶篇(6)

    PHP学习笔记- 进阶篇(6) 会话控制(session与cookie) 当前的Cookie为: cookie简介 Cookie是存储在客户端浏览器中的数据,我们通过Cookie来跟踪与存储用户数据. ...

  9. PHP学习笔记 - 进阶篇(4)

    PHP学习笔记 - 进阶篇(4) 字符串操作 字符串介绍 PHP开发中,我们遇到最多的可能就是字符串. 字符串变量用于包含字符串的值. 一个字符串 通过下面的3种方法来定义: 1.单引号 2.双引号 ...

随机推荐

  1. 300最长上升子序列 · Longest Increasing Subsequence

    [抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...

  2. [leetcode]199. Binary Tree Right Side View二叉树右视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  3. jquery之DataTables的使用

      jquery之DataTables的使用  document jquery function lsquo 强大的表格解决方案,有多强大,一起来看下吧: 1.DataTables的默认配置 $(do ...

  4. Django配置Bootstrap, js

    1.首先在APP目录下创建一个static文件夹 如图: # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'dj ...

  5. java Arrays.asList用法

    java Arrays.asList用法 用途 Arrays是java容器相关操作的工具类,asList方法将Array转换为list,是Array和List之间的桥梁. 注意 Arrays.asLi ...

  6. rpmdb open failed的解决办法

      错误信息如下:    “错误:无法从 /var/lib/rpm 打开软件包数据库      CRITICAL:yum.main:       Error: rpmdb open failed”   ...

  7. SQL思维导图

  8. common常用到的类

    org.apache.commons.codec.digest.DigestUtils.md5Hex(String)    md5

  9. c++编程思想里面的错误(可能c++标准变了,所以以前的东西没有更新)

    第一卷  第五章 5.3友元 下面的代码是<c++编程思想>里面的代码, struct X; struct Y{ void f(X*); }; struct X{ private: int ...

  10. 2018.07.18 HAOI2009 逆序对数列(线性dp)

    传送门 目前只会n2" role="presentation" style="position: relative;">n2n2的dp" ...