首页 技术 正文
技术 2022年11月16日
0 收藏 611 点赞 3,654 浏览 2183 个字

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

 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905