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 的目的是 ...
随机推荐
- nginx访问日志,错误日志参数说明
说明: nginx日志主要有两种:访问日志.错误日志.其中访问日志记录客户端访问nginx的每一个请求,包含用户地域来源.跳转来源.使用终端.某个URL访问量等信息,访问日志格式可以自定义:错误日志则 ...
- UITableView 滚动到最后一行
if (self.tableView.contentSize.height > self.tableView.frame.size.height) { CGPoint offset = CGPo ...
- 不要随便使用 runAllManagedModulesForAllRequests="true" 来解决问题
在 IIS 7.0 中,对于使用 Url 路由 访问页面的 ASP.NET 应用程序,IIS可能会不能出 Url 是对 ASP.NET 的请求. 会显示404啊,403啊之类的错误代码(因为路径不存在 ...
- 设置 debug 版本签名与生产版本一致
debug 版本使用生产版本的签名 在开发过程中,app 直接跑到手机上,用的签名文件是 Android Studio 默认的自动生成的一个签名,与生产版本的 app 签名是不一样的.当接入华为推送的 ...
- ASP.NET MVC中切换模板页(不同目录的cshtml文件)
看来以后建立一个父类控制器还是有必要的... using System;using System.Collections.Generic;using System.Linq;using System. ...
- haproxy-1.7.7 源码安装
安装一下依赖 yum install openssl-devel openssl 编译安装 make TARGET=linux2628 USE_OPENSSL=1 ADDLIB=-lz make in ...
- lua -- table.nums
table.nums 计算表格包含的字段数量. 格式: count = table.nums(表格对象) Lua 的“#”操作可以取得表格的长度,但仅限从 开始连续数字为索引的表格.table.num ...
- LoadingController
--local MainSceneConfig = require "res.scripts.configs.MainSceneConfig" -- 暂时添加一个临时配置文件 -- ...
- do-release-upgrade升级笔记
事后的总结: 后来可能因为阿里云镜像并不是完全底层无关镜像,do-release-upgrade后的18.04版本在经过一次异常的内核版本升级以后,restart失败,因为是虚机还很难处理,不得不直接 ...
- 【C/C++】标准IO操作
fwrite实现文件复制 //@ author 成鹏致远 //@ net http://infodown.tap.cn //@ qq 552158509 //@ blog lcw.cnblogs.co ...