require.js+knockout.js+.underscore模板引擎的使用
第一种使用方式:
HTML:
<ul data-bind="template: { name: 'peopleList' }"></ul> <script type="text/html" id="peopleList">
<% _.each(people(), function(person) { %>
<li>
<b data-bind="text: person.name"></b> is <%= person.age %> years old
</li>
<% }) %>
</script>
JS:
/* ---- Begin integration of Underscore template engine with Knockout. Could go in a separate file of course. ---- */
ko.underscoreTemplateEngine = function () { }
ko.underscoreTemplateEngine.prototype = ko.utils.extend(new ko.templateEngine(), {
renderTemplateSource: function (templateSource, bindingContext, options) {
// Precompile and cache the templates for efficiency
var precompiled = templateSource['data']('precompiled');
if (!precompiled) {
precompiled = _.template("<% with($data) { %> " + templateSource.text() + " <% } %>");
templateSource['data']('precompiled', precompiled);
}
// Run the template and parse its output into an array of DOM elements
var renderedMarkup = precompiled(bindingContext).replace(/\s+/g, " ");
return ko.utils.parseHtmlFragment(renderedMarkup);
},
createJavaScriptEvaluatorBlock: function(script) {
return "<%= " + script + " %>";
}
});
ko.setTemplateEngine(new ko.underscoreTemplateEngine());
/* ---- End integration of Underscore template engine with Knockout ---- */ var viewModel = {
people: ko.observableArray([
{ name: 'Rod', age: 123 },
{ name: 'Jane', age: 125 },
])
}; ko.applyBindings(viewModel);
第二种使用方式:
HTML代码段:
<div class="l_box_temp" id="side-userInfo"></div>
JS代码段:
define([
'knockout',
'jquery',
'underscore',
'model'
'text!home/view/side-userInfo.html' ], function(ko, $, _, model, viewBaozhang, template) { var ViewModel = function() {
var self = this;
self.viewUserInfo = $('#side-userInfo');
self.template = _.template(template);
self.html = '';
self.grade = '';
self.initialize = function() {
model.getData(function(res){
self.html = self.template(res);
self.viewUserInfo.html(self.html);
self.grade = res.grade;
setTimeout(function(){
ko.applyBindings(self, $('body')[0]);
},0) });
}
}
});
第二种方式的优点是可以从外部引入模板不需要把模板放在页面上,可以在多个页面重用。
require.js+knockout.js+.underscore模板引擎的使用的更多相关文章
- 在js里的ejs模板引擎使用
1.首先需要在页面里引用ejs.min.js. 2.将你的模板使用ejs编写,并存成后缀名.stmpl;(可能需要在打包工具里做些处理) 3.在js里使用require引入xxx.stmpl: con ...
- 比較Backbone.js, Angular.js, Ember.js, Knockout.js 心得
還記得第一次寫網站的時候,我無意間寫成了 SPA(single page application),當時還沒有SPA這個詞,後來因為廣告主需要不同 url location 頁面的廣告展示,只好把部分 ...
- Node.js学习(14)----EJS模板引擎
这个入门教程将从以下几个方面来讲解: 1. 引入EJS 2. 创建一个模板 3. 使用视图工具组件 4. 使用错误处理组件 5. 什么情况下应使用EJS 引入EJS 在我们正式开始前,我们先来做点准备 ...
- knockout.js模板绑定之利用Underscore.js模板引擎示例
View代码 <h1>People</h1> <ul data-bind="template: { name: 'peopleList' }"> ...
- js模板引擎介绍搜集
js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...
- node.js 使用ejs模板引擎时后缀换成.html
这是一个小技巧,看着.ejs的后缀总觉得不爽,使用如下方法,可以将模板文件的后缀换成我们习惯的.html. 1.在app.js的头上定义ejs: 代码如下: var ejs = require('ej ...
- JavaScript模板引擎Template.js使用详解
这篇文章主要为大家详细介绍了JavaScript模板引擎Template.js使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 template.js 一款 JavaScript 模板引 ...
- js模板引擎用法
JavaScript模板引擎Template.js使用详解 作者:A_山水子农 字体:[增加 减小] 类型:转载 时间:2016-12-15我要评论 这篇文章主要为大家详细介绍了JavaScript模 ...
- MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录
注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...
随机推荐
- Python 文件 isatty() 方法
概述 Python 文件 isatty() 方法检测文件是否连接到一个终端设备,如果是返回 True,否则返回 False. 语法 isatty() 方法语法如下: fileObject.isatty ...
- FreeSWITCH网关参数之caller-id-in-from
1. 这个配置项两个设置值: true和false(默认) <param name="caller-id-in-from" value="true"/&g ...
- Appium升级后安装UnicodeIME-debug.apk 提示
使用appium1.8 ,启动app过程中报错: 2018-05-08 17:09:16:890 - [W3C] Encountered internal error running command: ...
- java、oracle对CLOB处理
oracle CLOB字段转换位VARCHAR 1.实际上处理CLOB字段的时候,直接TO_CHAR,当长度超过4000的时候,会报错,提示列被截取: CLOB转varchar2:select to_ ...
- jQuery学习笔记(jquery.ui插件)
官网地址:http://ui.jquery.com/ jQuery UI源自于一jQuery插件-Interface.目前版本是1.10.3,需要jQuery 1.6以上版本支持. jQuery UI ...
- iconv 使用方法封装
std::string iconv_exec(const std::string& in, const char* fromcode, const char* tocode) { char b ...
- webapi中取文件的物理路径(server.mappath)
在Webapi中是用不了server.mappath方法的,上网搜索了一下,发现是用如下代码取得文件绝对路径: string logpath = System.Web.Hosting.HostingE ...
- ios 关于屏幕旋转和屏幕晃动
内置加速计是智能手机最酷的特性之一,ios可以通过这个小设备知道用户握持手机的方式,以及用户是否移动了手机,ios使用加速计处理自动旋转,并且许多游戏都是用它作为控制机制,它还可以用于检测摇动和其他突 ...
- [nginx]编译安装及安全优化
nginx配置-最后整理版 nginx_upstream_check_module nginx-module-vts nginx打补丁 nginx编译安装 - 下载 cd /usr/local/src ...
- HTML5学习笔记(三):语义化和新增结构元素
在HTML5之前,使用机器来阅读一个网页是非常困难的,我们使用不同样式的div来标记不同的内容,所以实际上机器无法得知页面的哪个部分是正文,哪个部分是标题,那么在HTML5里,针对这个问题就引入了语义 ...