学习在requirejs下如何使用underscore.js模板
近期在学习underscore.js 这个小而美的js库,是前端 MVC 框架backbone依赖库,他的模板方法主要应用场景是ajax交互过程到页面需要大量的字符串拼接,这部分如果一旦不够仔细就很容易拼错,而underscore.js的模板很好的解决了这个问题。自己做了个小小的练习,在requirejs下如何使用underscore.js模板。
首页un.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>underscore.js</title>
</head>
<body>
<div id="wraper">111</div>
<script data-main="js/un" src="require.js"></script> <!-- data-main 指定的入口函数 -->
<script type="text/javascript">
require.config({
baseUrl:'js',//指定目录
paths:{
jquery:'static/jquery-1.10.2.min',//指定jquery位置
underscore:'static/underscore' //指定underscore位置
},
urlArgs: "bust=" + (new Date()).getTime() //时间戳,清缓存的
})
</script>
<script type="text/template" id="tpl"><!-- underscore.js id是钩子 注意是text/template -->
<%_.each(obj,function(item){%>
<ul>
<li>
<a href="<%=item.url%>">
<%=item.name %>
</a>
<span><%=item.time%></span>
<%if(item.sub !== ''){%>
<%_.each(item.sub,function(k){%>
<b><a data-id=<%=k.id%> href="javascript:;" class="b"><%=k.name%></a></b>
<%})%>
<%}else{%> <%}%>
</li>
</ul>
<%})%>
</script>
</body>
</html>
我将requirejs 放到了 与 un.html平行的目录 具体到项目中 可能就不能这样放了。
下面是un.js
require(['jquery','underscore','cache'],function($,U,C){ //指定了依赖jq,underscore ,cache
C.putCode(); //应用cache.js的东西 练习用的 没有实际意义。
var data = [
{id:11,name:'aaa',age:31,url:'http://www.baidu.com',time:'12345',sub:[{id:'9999',name:'删除'},{id:8888,name:'删除'},{id:7777,name:'删除'}]},
{id:23,name:'bbb',age:66,url:'http://www.qiban365.com',time:'021545'},
{id:24,name:'ccc',age:99,url:'http://www.cnblogs.com',time:'45456456456'}
];
var text = _.template($('#tpl').html());
$("#wraper").html(text(data));
//$("#wraper").html(_.template($("#tpl").html(),data));//这个不好使
$(document).on('click','.b',function(){ //冒泡点击事件
console.log($(this).attr('data-id'));
if($(this).length !== ''){
$(this).remove();
}
})
})
学习在requirejs下如何使用underscore.js模板的更多相关文章
- knockout.js模板绑定之利用Underscore.js模板引擎示例
View代码 <h1>People</h1> <ul data-bind="template: { name: 'peopleList' }"> ...
- Underscore.js 源码学习笔记(上)
版本 Underscore.js 1.9.1 一共 1693 行.注释我就删了,太长了… 整体是一个 (function() {...}()); 这样的东西,我们应该知道这是一个 IIFE(立即执行 ...
- Underscore.js 初探
一. 简介 Underscore 这个单词的意思是“下划线”. Underscore.js 是一个 JavaScript 工具库,提供了一整套的辅助方法供你使用. Think that - ...
- Underscore.js 源码学习笔记(下)
上接 Underscore.js 源码学习笔记(上) === 756 行开始 函数部分. var executeBound = function(sourceFunc, boundFunc, cont ...
- underscore.js学习笔记
一.理清概念 1.Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率,Underscore还可以被使用在Node.js运行环境.从API中,你已经可以看出,Unders ...
- 如何应用r.js对requirejs下的js代码合并
1.在根目录新建build.js ({ baseUrl:'js', paths:{ jquery:'static/jquery-1.10.2.min', underscore:'static/unde ...
- 【Knockout.js 学习体验之旅】(3)模板绑定
本文是[Knockout.js 学习体验之旅]系列文章的第3篇,所有demo均基于目前knockout.js的最新版本(3.4.0).小茄才识有限,文中若有不当之处,还望大家指出. 目录: [Knoc ...
- underscore.js 源码
underscore.js 源码 underscore]JavaScript 中如何判断两个元素是否 "相同" Why underscore 最近开始看 underscore.js ...
- Underscore.js 的模板功能介绍与应用
Underscore是一个非常实用的JavaScript库,提供许多编程时需要的功能的支持,他在不扩展任何JavaScript的原生对象的情况下提供很多实用的功能,需要了解的朋友可以详细参考下 U ...
随机推荐
- C++ 学习笔记(三)string 类
在C语言中如果想要使用字符串那么有两种方法: 1.定义char型数组:char[10]; 然后将每个字符填充到对应的位置. 优点:这种方式将字符串放在内存所以每个位置都可以修改. 缺点:赋值比较麻烦, ...
- 【SAM】bzoj5084: hashit
做得心 力 憔 悴 Description 你有一个字符串S,一开始为空串,要求支持两种操作 在S后面加入字母C 删除S最后一个字母 问每次操作后S有多少个两两不同的连续子串 Input 一行一个字符 ...
- (71)Received empty response from Zabbix Agent问题解决
刚接触zabbix新手少部分会出现如下错误: Received empty response from Zabbix Agent at [192.168.1.2]. Assuming that age ...
- python入门:print打印输出的用法
#!/usr/bin/python # -*- coding:utf-8 -*- # print打印输出的用法 print("holle,world!") print(1) pri ...
- paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之output encoded style with registered outputs(Good style)
把输出跟状态编码结合起来,即使可以省面积又是寄存器输出.但是没有讲解如何实现这种高效的编码.
- COMP9021--6.17
1. ''' '''the comment in the middle will be shown in your code while ranning 2. a=bc=a%bor we can si ...
- for_each_node(node)
遍历各个pg_data_t节点. 1.定义在include/linux/nodemask.h中 /* * Bitmasks that are kept for all the nodes. */ en ...
- stm32L0系列学习(二)HAL-LL库等比较
- Memcached配置失误引发的Keystone token丢失的问题
故障现象 近期公司的OpenStack上频繁出现虚拟机创建失败的情况,查看日志定位到问题出在neutron-server向keystone认证token失败. 故障原因 Keystone所使用的Mem ...
- session工作原理
什么是Sesson? 这个是状态保持三大对象之一! 原意是会话,会议的意思! 就是你打开浏览器到关闭浏览器 这期间称为一个会话,也就是一个session, 它是保存在服务器端的. 每当客户端请求页面时 ...