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学习笔记之一的更多相关文章

  1. knockout学习笔记目录

    关于knockout学习系列的文章已经写完,这里主要是做个总结,并且将目录罗列出来,方便查看.欢迎各位大神拍砖和讨论. 总结 kncokout是一个轻量级的UI类库,通过MVVM模式使前端的UI简单话 ...

  2. knockout学习笔记10:demo

    前面已经介绍了ko的基本用法,结合官方文档,基本就可以实际应用了.本章作为ko学习的最后一篇,实现一个简单的demo.主要集中在ko,所以后台数据都是静态的.类似于博园,有一个个人文章的分类列表,一个 ...

  3. Knockout学习笔记之二($root,$parent及$data的区别)

    以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下: 对于$root 与$parent的区别: $root refers to the view model appl ...

  4. KnockoutJs学习笔记(五)

    作为一名初学者来说,一篇篇的按顺序看官网上的文档的确是一件很痛苦的事情,毕竟它的排列也并非是由浅及深的排列,其中的顺序也颇耐人寻味,于是这篇文章我又跳过了Reference部分,进而进入到具体的bin ...

  5. Knockout.js快速学习笔记

    原创纯手写快速学习笔记(对官方文档的二手理解),更推荐有时间的话读官方文档 框架简介(Knockout版本:3.4.1 ) Knockout(以下简称KO)是一个MVVM(Model-View-Vie ...

  6. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  7. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  8. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  9. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

随机推荐

  1. Cell的一些坑: UITableViewCell宽度,在iphone5的时候是320,在iphone6的时候为啥也是320?

    在自定制cell'的.m文件里重写setframe方法就可以了- (void)setFrame:(CGRect)frame{    frame.size.width = [UIScreen mainS ...

  2. ios-自定义alertView提示框

    先上图,弹框的背景色,按钮背景色,提示的消息的字体颜色都可以改变 利用单例实现丰富的自定义接口 // // PBAlertController.h // PBAlertDemo // // Creat ...

  3. PRML读书笔记——Mathematical notation

    x, a vector, and all vectors are assumed to be column vectors. M, denote matrices. xT, a row vcetor, ...

  4. 并发案例--ScheduledExecutorService用法

    InstanceFactory.getInstance(ScheduledExecutorService.class).schedule(new Callable<Object>() { ...

  5. .net下的跨域问题

    环境: IIS7.0 MVC 4.0     公司官网 asp.net      需要的报名系统,需要有后台管理 由于是配合传统产业,所以MVC系统的数据,是由AIPS系统提供. (制作前是考虑去年用 ...

  6. http.Handler 与Go的错误处理

    原文地址    在之前我写过一篇关于通过使用http.HandlerFunc来实现一个定制handler类型用来避免一些平常的错误的文章.func MyHandler(w http.ResponseW ...

  7. 学习OpenCV——SVM 手写数字检测

    转自http://blog.csdn.net/firefight/article/details/6452188 是MNIST手写数字图片库:http://code.google.com/p/supp ...

  8. 对java多线程的认识

    多线程的概念:多线程是一种机制,它允许在程序中并发的执行多个线程,且每个线程间相互独立. 实现多线程的两种方式: 1.继承java.lang.Thread类,并且重写它的run方法,将线程的执行主体放 ...

  9. 让Docker容器使用静态独立的外部IP(便于集群组建)

    需要使用Docker虚拟化Hadoop/Spark等测试环境,并且要可以对外提供服务,要求是完全分布式的部署(尽量模拟生产环境).那么我们会遇到几个问题: Container IP 是动态分配的 Co ...

  10. js 格式化数字保留2位小数

    function toDecimal2(x) { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x ...