Visible binding会依据绑定的数据来决定相应的DOM元素是否隐藏,hidden或visible。javascript
咱们首先在js文件部分定义一个view model,这里我建立的是一个object而不是一个构造函数,我的分析认为,object类型的定义就是一种persistent view model,而构造函数类型的定义就是一种temporary view model,这个也许和pure computed observables中的性能有关,可和knockout学习笔记(四)联系起来。css
<div data-bind="visible: shouldShowMessage"> You will see this message only when "shouldShowMessage" holds a true value. </div> <script> var viewModel = { shouldShowMessage: ko.observable(true) // Message initially visible }; viewModel.shouldShowMessage(false); //隐藏元素 //viewModel.shouldShowMessage(true); //显示元素 ko.applyBindings(viewModel); </script>
Text binding会使得相应的DOM element的text部分变为参数的值。html
<p>This is my text: <span data-bind="text: myText"></span></p> <script> var myViewModel = { myText: ko.observable() }; ko.applyBindings(myViewModel); myViewModel.myText("Hello world!"); ko.applyBindings(viewModel); </script>
输出结果:java
This is my text: Hello world!web
<p>This is my text: <span data-bind="text: myComputed"></span></p> <script type="text/javascript"> var viewModel = { myText: ko.observable(60), }; viewModel.myComputed = ko.pureComputed(function () { return this.myText() > 50 ? "Big" : "Small"; }, viewModel); ko.applyBindings(viewModel); </script>
输出结果:app
This is my text: Big函数
若是咱们传入text的参数中含有html的标签,页面中并不会将其以标签的形式展示,而是会以字符串的方式展示出来,例如:性能
myViewModel.myText("<i>Hello world!</i>");
输出结果:学习
This is my text: <i>Hello world!</i>
this
这样也能够防止HTML或是script的注入攻击。而有关html标签的绑定则须要参考html binding
在某些时候,咱们须要在不指定元素的状况下直接插入text,此时咱们可使用无容器的语法来定义一个text binding。例如在option element中是不容许包含其余元素的,若是咱们以下定义data-bind
<select> <option>Item <span data-bind="text: myText"></span></option> </select>
咱们并不能获得view model中的observable,无容器的语法则是这样写的:
<select data-bind="foreach: myItems"> <option>Item <!--ko text: name--><!--/ko--></option> </select>
<!--ko-->
和<!--/ko-->
扮演着开始标志和结束标志,它们定义了一个虚拟元素,knockout语法可以理解这种写法并像对待一个最真实元素那样去绑定相应的view model内的值。
完整的案例:
<select data-bind="foreach: myItems"> <option>Item <span data-bind="text: name"></span></option> </select> <select data-bind="foreach: myItems"> <option>Item <!--ko text: name--><!--/ko--></option> </select> <script type="text/javascript"> var vm = { myItems: [ { name: 'Item 1', id: 1}, { name: 'Item 3', id: 3}, { name: 'Item 4', id: 4} ] }; ko.applyBindings(vm); </script>
Html binding其实与text binding相似,只不过它的参数通常是带有html标签的,这样就能够自定义想要绑定的html元素。
下面是个很简单的例子:
var myViewModel = { myHtml: ko.observable(), } ko.applyBindings(myViewModel); myViewModel.myHtml("<a href='http://www.google.com'>Google</a>");
html
<p>My html element is: <span data-bind="html: myHtml"></span></p>
<div data-bind="class: profitStatus"> Profit Information </div> <script type="text/javascript"> var viewModel = { currentProfit: ko.observable(150000) //<div data-bind="class: profitStatus" class="profitPositive"> }; // Evalutes to a positive value, so initially we apply the "profitPositive" class viewModel.profitStatus = ko.pureComputed(function() { return this.currentProfit() < 0 ? "profitWarning" : "profitPositive"; }, viewModel); // Causes the "profitPositive" class to be removed and "profitWarning" class to be added viewModel.currentProfit(-50); //<div data-bind="class: profitStatus" class="profitWarning"> ko.applyBindings(viewModel); </script>
改变的是class的值。
CSS binding主要用于根据view model的修改来更改UI中相应元素的class,从而依照CSS文件中已经定义好的样式来体如今页面中。一个简单的static css binding例子以下:
<style> .redText { color: red; } </style> </head> <body> <p data-bind="css: { redText: myTest() > 0 }">Test for css binding</p> <script type="text/javascript"> var myViewModel = { myTest: ko.observable() }; myViewModel.myTest(20); console.info(myViewModel.myTest()); ko.applyBindings(myViewModel); </script>
另外,咱们也能够经过绑定一个computed observable来动态指定css class的值,这样的绑定称为dynamic css binding,简单的例子以下:
<style> .redText { color: red; } .blueText { color: blue; } </style> <p id="redText" data-bind="css: myComputed">This is red text.</p> <p id="blueText" data-bind="css: myComputed">This is blue text.</p> <script type="text/javascript"> var myViewModel1 = { myTest: ko.observable(), myComputed: ko.pureComputed(function() { return myViewModel1.myTest() > 0 ? "redText" : "blueText"; }) }; var myViewModel2 = { myTest: ko.observable(), myComputed: ko.pureComputed(function() { return myViewModel2.myTest() > 0 ? "redText" : "blueText"; }) }; ko.applyBindings(myViewModel1, document.getElementById("redText")); ko.applyBindings(myViewModel2, document.getElementById("blueText")); myViewModel1.myTest(20); myViewModel2.myTest(-20); </script>
css bidning是十分灵活的,对于static css binding,咱们通常是以data-bind="css: {cssClass: viewModelProperty}
"的形式来绑定css class,cssClass会根据viewModelProperty返回的值是 true
仍是 false
来决定这个class如今是否须要使用。另外,咱们也能够一次性设置多个css classes,简单的示例以下:
<style> .redText { color: red; } .textDecoration { text-decoration: underline; } </style> <p data-bind="css: {redText: redTextDecision, textDecoration: textDecorationDecision}" > This is red text, and its text-decoration is underline. </p> <script type="text/javascript"> var myViewModel = { redTextDecision: ko.observable(), textDecorationDecision: ko.observable() }; ko.applyBindings(myViewModel); myViewModel.redTextDecision(true); myViewModel.textDecorationDecision(true); </script>
在上例中,咱们是使用两个view model的property来决定两个不一样的css class,KO也容许咱们使用一个view model property来决定多个css class,可将上例的html部分改为以下形式:
<p data-bind="css: {'redText textDecoration': redTextDecision}">This is red text, and its text-decoration is underline.</p>
这里为了简便,并无更改相应的view model property的名称,能够看到,当对多个css class进行设定的时候,咱们须要将它们统一包含在引号中。
若是绑定的view model property属性是一个observable,则UI会根据该observable的变化来动态的增长或删去被绑定的css class,不然,UI只会在开始阶段设定一次css class,以后再也不更新。
dynamic css binding的通常形式是这样的:data-bind="css: viewModelProperty"
,UI会根据viewModelProperty
返回的字符串(property自己能够是一个表明css class的字符串)来决定添加的css class的名称。若是property是一个observable,则在该observable改变的时候,UI会删去以前该observable所添加的class并从新设置为当前observable的值。在dynamic css binding中,咱们可让viewModelProperty添加多个css class,只需将这些css class以空格分开并统一以字符串的方式返回便可。一个简单的例子以下:
<style> .redText { color: red; } .textDecoration { text-decoration: underline; } </style> <p data-bind="css: textDecorationDecision"> This is red text, and its text-decoration is underline. </p> <script type="text/javascript"> var myViewModel = { textDecorationDecision: ko.observable("textDecoration redText") }; ko.applyBindings(myViewModel); </script>
对于某些带有特殊符号的css class,好比red-text,咱们不能直接在data-bind中以data-bind="css: {red-text: redTextDecision}"的方式来设定,而是应该在这类css class外添加一对引号使其成为字符串常量,变成这样:data-bind="css: {'red-text': redTextDecision}"。
若是你想套用 CSS 类my-class你 ,不能这样写 :
<div data-bind="css: { my-class: someValue }">...</div>
由于个人类在这一点上不是一个合法的标识符名称。解决方案很简单: 只需用引号包装标识符名称, 使其成为字符串文本, 这在 javascript 对象文本中是合法的。例如,
<div data-bind="css: { 'my-class': someValue }">...</div>
style binding是与css binding不一样的控制页面样式的方式,css binding须要依赖css文件中对应的样式表,借由增长或删除css class来改变样式,而style binding则是直接在相应的元素上添加或删除style来改变页面的样式。一个简单的例子以下:
<p data-bind="style: {color: redTextDecision}">This is red text.</p> <script type="text/javascript"> var myViewModel = { redTextDecision: ko.observable("red") }; ko.applyBindings(myViewModel); </script>
与css binding相似,咱们也能够一次性设定多个style,以下:
<p data-bind="style: {color: redTextDecision, align: alignDecision}">This is red text.</p>
若是绑定的viewModelProperty是一个observable,则每次observable改变的同时,UI会对对应的style进行更新;不然,UI只会设定一次style,以后再也不改变。
有些style是带有特殊符号的,好比"text-decoration",好比"font-size"。在这种状况下,直接在data-bind中填写style的原名是不妥的,咱们须将其变动为"textDecoration"和"fontSize",其余相似的情形以此类推。
Don’t write
{ font-weight: someValue };
do write{ fontWeight: someValue }
Don’t write{ text-decoration: someValue };
do write{ textDecoration: someValue }
attr binding用于绑定某些元素的attribute,好比说a元素的title、href,img元素的src等。一个简单的例子以下:
<a data-bind="attr :{ href: url, title: description}">Google's website</a> <img style="width: 100px; height: 100px" data-bind="attr: {src: imgUrl}" /> <script type="text/javascript"> var myViewModel = { url: ko.observable("http://www.google.com"), description: ko.observable("This is google's website"), imgUrl: ko.observable("http://i1.sinaimg.cn/dy/deco/2013/0329/logo/LOGO_1x.png") }; ko.applyBindings(myViewModel); </script>
若是绑定的viewModelProperty是一个observable,则每次observable改变时,UI均会更新相应的attribute;不然,UI只会设定一次attribute,以后再也不更新。
当attribute name中带有连字符号时,咱们只需在其外部添加一对引号便可。
CharlieYuki,KnockoutJs学习笔记(五)