Knockoutjs 3.X API Fourth Chapter data Control flow with binding _javascript tips

Source: Internet
Author: User

The purpose of the with binding

Using the with binding format is data-bind= "With:attribute", using the with binding will treat the subsequent property as a new context binding. All elements within the with binding are bound by that context.

Of course, with bindings can also be used with if or foreach bindings.

Example 1

<H1 data-bind= "text:city" >  
 

In this case, the Coords monitoring property is directly bound with the with, and the internal properties of the Coords monitoring property are called directly inside it. This shows the characteristics of the with binding.

Example 2: An interactive example

This example uses the with binding to dynamically add and remove its bound value as null/undefined or null/undefined

UI Source code:

<form data-bind= "Submit:gettweets" >
Twitter Account:
<input data-bind= "Value:twittername"/>
<button type= "Submit" >get tweets</button>
</form>
<div data-bind= "with: Resultdata ">
 
 

View Model Source:

function Appviewmodel () {
var self = this;
Self.twittername = ko.observable (' @example ');
Self.resultdata = Ko.observable (); No Initial value
self.gettweets = function () {
var name = Self.twittername (),
simulatedresults = [
{te  Xt:name + ' What a nice Day '},
{text:name + ' building some cool apps. '},
{text:name + ' Just saw a famous Celebrity eating lard. Yum. '}
];
Self.resultdata ({retrievaldate:new Date (), toptweets:simulatedresults});
Self.clearresults = function () {
self.resultdata (undefined);
}
}
Ko.applybindings (New Appviewmodel ());

Note: With container-free binding (virtual binding)

Like the virtual bindings of If or foreach, the with binding is the same. Use the <!--ko--> and <!--/ko-->.

<ul>
<li>header element</li>
<!--ko with:outboundflight-->
...
<!--/ko-->
<!--ko with:inboundflight-->
...
<!--/ko-->
</ul>

The above is a small set to introduce the Knockoutjs 3.X API Fourth chapter of the data control flow with binding, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.