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. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  2. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

  3. Android You need to use a Theme.AppCompat theme (or descendant) with this activity.

    错误描述为:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...

  4. (copy) DBAN vs nwipe

    source: https://sourceforge.net/p/dban/discussion/208932/thread/cb591b59/ Question:Trouble in runnin ...

  5. Java 多线程submit和execute

    submit方法: public abstract class AbstractExecutorService implements ExecutorService { protected <T ...

  6. [PCL]FPFH描述子

    fpfh_est.setSearchSurface(data); 如果没有设置SearchSurface,使用input数据集作为查找的表面 // If no search surface has b ...

  7. 移动端html页面布局

    记录一下: ---------- 如果设计稿尺寸为 : 96px: 1. rem html{ font-size:62.5%; } 的情况下 : 4.8rem: 2. rem html{ font-s ...

  8. Python 面向对象编程进阶

    静态方法 只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在 ...

  9. 史上最易懂的Android jni开发资料--NDK环境搭建

    谷歌改良了ndk的开发流程,对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本,必须要安装Cygwin才能使用NDK.而在NDKr7开始,Google的Windows版的NDK提供 ...

  10. HTML5中的时间类型,另外EL表达式的时间值来读取时间,并且还可以更改时间

    HTML5规范里只规定date新型input输入类型,并没有规定日历弹出框的实现和样式.所以,各浏览器根据自己的设计实现日历.目前只有谷歌浏览器完全实现日历功能.相信这种局面很快就会结束,所有的浏览器 ...