Knockout学习笔记之一
1. 四大关键理念:
A. DeclarativeBindings(声明式绑定)
Easily associate DOM elements with model data using a concise, readable syntax
使用简单易读的语法方便地将模型数据与DOM元素绑定在一起
B. AutomaticUIRefresh(页面自动刷新)
When your data model's state changes, your UI updates automatically
C. DependencyTracking(依赖追踪)
Implicitly set up chains of relationships between model data, to transform and combine it
使用可观察对象在模型数据之间设立隐性关系链,用于数据转换和绑定。
D. Templating(模板)
Quickly generate sophisticated, nested UIs as a function of your model data
内置模板引擎、为你的模型数据快速编写复杂的 UI 展现
2. 声明式绑定
声明式绑定即它的声明的同时也进行了绑定(自己的理解)。
3. applyBindings
Activates knockout.js -ko.applyBindings(new AppViewModel());
激活knockout.js(即激活data-bind属性)
4. observables - these are properties that automatically will issue notifications whenever their(View Models) value changes
双向绑定,当ViewModel中的值发生变化时,Dom元素的值也会相应地发生变化,反之亦然。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="knockout-3.4.0.js"></script>
<script type="text/javascript">
window.onload = function ()
{
ko.applyBindings(new AppViewModel());
} function AppViewModel()
{
this.firstName = ko.observable("Hello ");
this.lastName = ko.observable("World!");
this.fullName = ko.computed(function () {
return this.firstName() + " " + this.lastName();
},this);
this.capitalizeLastName = function ()
{
var currentVal = this.lastName();
this.lastName(currentVal.toUpperCase());
}
} </script>
</head>
<body>
<P>First Name:<input data-bind="value: firstName" /></P>
<P>Last Name:<input data-bind="value: lastName" /></P>
<p>FullName:<input data-bind="value: fullName" /></p>
<button data-bind="click: capitalizeLastName">Go Caps</button>
</body>
</html>
5. knockout例子(座位预订)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="knockout-3.4.0.js"></script>
<script type="text/javascript">
function SeatReservation(name,initialMeal)
{
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
self.formattedPrice = ko.computed(function () {
var price = self.meal().price;
return price ? "$" + price.toFixed(2) : "None";
});
} function ReservationsViewModel()
{
var self = this;
self.availableMeals = [
{ mealName: "Standard(Sandwich)", price: 0 },
{ mealName: "Premium:(lobster)", price: 34.95 },
{ mealName:"Ultimate(whole zebra)",price:290}
];
self.seats = ko.observableArray([
new SeatReservation("Steve", self.availableMeals[0]),
new SeatReservation("Bert", self.availableMeals[0])
]); self.addSeat = function () {
self.seats.push(new SeatReservation("", self.availableMeals[0]));
}; self.removeSeat = (function (seat) {
self.seats.remove(seat);
}); self.totalSurcharge = ko.computed(function () {
var total = 0;
for (var i = 0; i < self.seats().length; i++)
{
total += self.seats()[i].meal().price;
}
return total;
})
}
window.onload = function () {
ko.applyBindings(new ReservationsViewModel());
} </script>
</head>
<body>
<h2>Your seat reservations</h2>
<table>
<thead>
<tr>
<th>Passenger name</th>
<th>Meal</th>
<th>Surcharge</th>
</tr>
</thead>
<tbody data-bind="foreach:seats">
<tr>
<td><input data-bind="value:name"/></td>
<td><select data-bind="options:$root.availableMeals,value:meal,optionsText:'mealName'"></select></td>
<td data-bind="text:formattedPrice"></td>
<td><a href="#" data-bind="click:$root.removeSeat">Remove</a></td>
</tr>
</tbody>
</table>
<h3 data-bind="visible:totalSurcharge()>0">
Total surcharge:$<span data-bind="text:totalSurcharge().toFixed(2)"></span>
</h3>
<button data-bind="click:addSeat,enable:seats().length<10">Reserve another seat</button> </body>
</html>
6.Knockout使用<!--ko--><!--/ko-->来表示循环的开始和结束;
切记不是注释!
7. 总结分析:
问题一:在第一个例子中,在调用ko.computed()方法时,第二个参数this的作用?
答:这个和JS中的基本一致,是为了ko.computed()方法内部的使用而传入的;
问题二:在第二个例子中,什么时候用meal,什么时候用meal()?(以下为官网说法)
Notice that, because the meal property is an observable, it's important to invoke meal() as a function (to obtain
its current value) before attempting to read sub-properties. In other words, write meal().price, not meal.price.
因为meal的属性是observable,在获取该类型当前值时,必须将其作为一个函数来使用,即 meal() ,换句话说就是meal().price,而不是meal.price。
Knockout学习笔记之一的更多相关文章
- knockout学习笔记目录
关于knockout学习系列的文章已经写完,这里主要是做个总结,并且将目录罗列出来,方便查看.欢迎各位大神拍砖和讨论. 总结 kncokout是一个轻量级的UI类库,通过MVVM模式使前端的UI简单话 ...
- knockout学习笔记10:demo
前面已经介绍了ko的基本用法,结合官方文档,基本就可以实际应用了.本章作为ko学习的最后一篇,实现一个简单的demo.主要集中在ko,所以后台数据都是静态的.类似于博园,有一个个人文章的分类列表,一个 ...
- Knockout学习笔记之二($root,$parent及$data的区别)
以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下: 对于$root 与$parent的区别: $root refers to the view model appl ...
- KnockoutJs学习笔记(五)
作为一名初学者来说,一篇篇的按顺序看官网上的文档的确是一件很痛苦的事情,毕竟它的排列也并非是由浅及深的排列,其中的顺序也颇耐人寻味,于是这篇文章我又跳过了Reference部分,进而进入到具体的bin ...
- Knockout.js快速学习笔记
原创纯手写快速学习笔记(对官方文档的二手理解),更推荐有时间的话读官方文档 框架简介(Knockout版本:3.4.1 ) Knockout(以下简称KO)是一个MVVM(Model-View-Vie ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
随机推荐
- PRAGMA AUTONOMOUS_TRANSACTION
转自 http://blog.csdn.net/pan_tian/article/details/7675800 这段时间遇到一个问题,程序里明明插入了一条记录,但在后边的一段Procedure中却查 ...
- elk实战分析nginx日志文档
elk实战分析nginx日志文档 架构: kibana <--- es-cluster <--- logstash <--- filebeat 环境准备:192.168.3.1 no ...
- HTTP协议 keep-alive连接 与 BS(firefox-thttpd)实验
什么是 keep-alive 连接 https://en.wikipedia.org/wiki/HTTP_persistent_connection HTTP persistent connectio ...
- smartdraw2013破解方法
smartdraw是一个非常好的画图工作,最大的优点就是支持多种图形,采用模板的方式在线扩充,可以快速画出你想要的图形,具体的介绍见其他资料. 这里是我自己的破解办法. 网上的下载都包含破解工具,但是 ...
- Leetcode: Nested List Weight Sum II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- Leetcode: Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- HTML 5 Canvas 参考手册
HTML 5 Canvas 参考手册 HTML 视频/音频 HTML 文档类型 描述 HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript). 不过,&l ...
- 将n*n矩阵顺时针旋转90度
/** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...
- Java集合---ArrayList的实现原理
目录: 一. ArrayList概述 二. ArrayList的实现 1) 私有属性 2) 构造方法 3) 元素存储 4) 元素读取 5) 元素删除 6) 调整数组容量 ...
- C#中把Datatable转换为Json的5个代码实例
一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table" ...