Jquery template technology and data binding implementation code

Source: Internet
Author: User
Tags javascript array

The good news is that Microsoft has developed such a framework, which is an extension of jquery, from then on, it will be as easy to dynamically display the data returned by the web service on the server side in the browser.

This technology is called jquery templates and data linking, which is an extension of jquery by Microsoft. This type of extension requires jquery's official review before it can be included in jquery. According to scottgu's blog, this extension is awaiting review. However, these functions are very powerful and practical. I will turn them around first, and the first thing I can do is get fast.

When writing Ajax applications, JavaScript is often used to display data on pages. jquery templates and data linking) it provides a convenient way to display JavaScript data on HTML pages. data can be displayed as various elements, such as plain text, Textbox, and list. The literal translation of the word "data linking" should be used as a data link, but I think the word "Data Binding" can better reflect the ability of this technology and comply with ASP. NET usage. In Scott Guthrie's blog, they also wanted to call this technology data binding instead of data linking, but since jquery already has the term binding, it refers to another thing. In order not to cause confusion, we have to give it another name, data linking.

I will transfer this blog here. If you have time, translate it.

The following full text is from scottgu's blog.

Jquery templates and data linking (and Microsoft contributing to jquery)

The jquery library has a passionate community of developers, and it is now the most widely used JavaScript library on the Web today.

Two years ago I announced that Microsoft wowould begin offering product support for jquery, and that we 'd be including it in new versions of Visual Studio going forward. by default, when you create new ASP. net web forms and ASP. net MVC projects with vs 2010 you'll find jquery automatically added to your project.

A few weeks ago during my second keynote at the MIX 2010 Conference I announced that Microsoft wowould also begin contributing to the jquery project. during the talk, john resig -- the creator of the jquery library and leader of the jquery developer team-talked a little about our participation and discussed an early prototype of a new client templating API for jquery.

In this blog post, I'm going to talk a little about how my team is starting to contriures TO THE jquery project, and discuss some of the specific features that we are working on suchClient-side TemplatingAndData linking(Data-binding ).

Contributing to jquery

Jquery has a fantastic developer community, and a very open way to propose suggestions and make contributions. Microsoft is following the same process to contribute to jquery as any other member of the community.

As an example, when working with the jquery community to improve support for templating to jquery my team followed the following steps:

    1. We created a proposal for templating and posted the proposal to the jquery Developer Forum (http://forum.jquery.com/topic/jquery-templates-proposal and http://forum.jquery.com/topic/templating-syntax ).
    2. After processing ing feedback on the forums, the jquery team created a prototype for templating and posted the prototype at the GitHub code repository (http://github.com/jquery/jquery-tmpl ).
    3. We iterated on the prototype, creating a new fork on GitHub of the templating prototype, to suggest design improvements. Several other members of the community also provided design feedback by forking the templating code.

There has been an amazing amount of particle by the jquery community in response to the original templating proposal (over 100 posts in the jquery Forum ), and the design of the templating proposal has evolved significantly based on community feedback.

The jquery team is the ultimate determiner on what happens with the templating proposal-they might include it in jquery core, or make it an official plugin, or reject it entirely. my team is excited to be able to participate in the open source process, and make suggestions and contributions the same way as any other member of the community.

Jquery template support

Client-side templates enable jquery developers to easily generate and render html ui on the client. templates support a simple syntax that enables either developers or designers to declaratively specify the HTML they want to generate. developers can then programmatically invoke the templates on the client, and pass JavaScript objects to them to make the content rendered completely data driven. these JavaScript objects can optionally be based on data retrieved from a server.

Because the jquery templating proposal is still evolving in response to community feedback, the final version might look very different than the version below. this blog post gives you a sense of how you can try out and use templating as it exists today (you can download the prototype by the jquery core team at http://github.com/jquery/jquery-tmpl or the latest submission from my team at http://github.com/nje/jquery-tmpl ).

Jquery client templates

You createClient-sideJquery templates by embedding content within a <SCRIPT type = "text/html"> tag. For example, the HTML below contains a <div> template container, as well asClient-sideJquery "contacttemplate" template (within the <SCRIPT type = "text/html"> element) that can be used to dynamically display a list of contacts:

The {= Name }}and {= phone} expressions are used within the contact template above to display the names and phone numbers of "contact" objects passed to the template.

We can use the template to display either an array of JavaScript objects or a single object. the javascript code below demonstrates how you can render a JavaScript array of "Contact" Object using the above template. the render () method renders the data into a string and appends the string to the "contactcontainer" Div element:

When the page is loaded, the list of contacts is rendered by the template. All of this template rendering is happening on the client-side within the browser:

Templating commands and conditional display logic

The current templating proposal supports a small set of template commands-includingIf,Else, AndEachStatements. The number of template commands was deliberately kept small to encourage people to place more complicated logic outside of their templates.

Even this small set of template commands is very useful though. Imagine, for example, that each contact can have zero or more phone numbers. The contacts cocould be represented by the Javascript array below:

The template below demonstrates how you can useIfAndEachTemplate commands to conditionally display and loop the phone numbers for each contact:

If a contact has one or more phone numbers then each of the phone numbers is displayed by iterating through the phone numbers withEachTemplate command:

The jquery team designed the template commands so that they are eXtensible. If you have a need for a new template command then you can easily add new template commands to the default set of commands.

Support for client data-linking

The ASP. NET team recently submitted another proposal and prototype to the jquery forums (http://forum.jquery.com/topic/proposal-for-adding-data-linking-to-jquery). This proposal describes a new feature namedData linking. Data linkingEnables you to link a property of one object to a property of another object-so that when one property changes the other property changes. data linking enables you to easily keep your UI and Data Objects synchronized within a page.

If you are familiar with the concept of data-binding then you will be familiar with data linking (in the proposal, we call the feature data linking because jquery already des a BIND () method that has nothing to do with data-binding ).

Imagine, for example, that you have a page with the following HTML <input> elements:

The following JavaScript code links the two input elements above to the properties of a JavaScript "Contact" object that has a "name" and "phone" property:

When you execute this code, the value of the first input element (# name) is set to the value of the contactNameProperty, and the value of the second input element (# Phone) is set to the value of the contactPhoneProperty. The properties of the contact object and the properties of the input elements are also linked-so that changes to one are also reflected in the other.

Because the contact object is linked to the input element, When you request the page, the values of the contact properties are displayed:

More interesting, the values of the linked input elements will change automatically whenever you update the properties of the contact object they are linked.

For example, we cocould programmatically modify the properties of the "Contact" Object usingJquery ATTR ()Method like below:

Because our two input elements are linked to the "Contact" object, the input element values will be updated automatically (without us having to write any code to modify the UI elements ):

Note that we updated the contact object above using the jquery ATTR () method. In order for data linking to work, you must use jquery methods to modify the property values.

Two way linking

The linkboth () method enablesTwo-wayData linking. The contact object and input elements are linked in both directions ctions. When you modify the value of the input element, the contact object is also updated automatically.

For example, the following code adds a client-side JavaScript click handler to an HTML button element. when you click the button, the property values of the contact object are displayed using an alert () dialog:

The following demonstrates what happens when you change the value of the name input element and click the Save button. notice that the name property of the "Contact" object that the input element was linked to was updated automatically:

The above example is obviusly trivially simple. instead of displaying the new values of the contact object with a javascript alert, you can imagine instead calling a web-service to save the object to a database. the benefit of data linking is that it enables you to focus on your data and frees you from the mechanics of keeping your UI and data in sync.

Converters

The current data linking proposal also supports a feature calledConverters. A converter enables you to easily convert the value of a property during data linking.

For example, imagine that you want to represent phone numbers in a standard way with the "Contact" Object phone property. in particle, you don't want to include special characters such as ()-In the phone number-Instead you only want digits and nothing else. in that case, you can wire-up a converter to convert the value of an input element into this format using the code below:

Notice above how a converter function is being passed to the linkfrom () method used to link the phone property of the "Contact" object with the value of the phone input element. this convertor function strips any non-numeric characters from the input element before updating the phone property. now, if you enter the phone number (206) 555-9999 into the phone input field then the value 2065559999 is assigned to the phone property of the contact object:

You can also use a converter in the opposite direction also. For example, you can apply a standard phone format string when displaying a phone number from a phone property.

Combining templating and data linking

Our goal in submitting these two proposals for templating and data linking is to make it easier to work with data when building websites and applications with jquery. templating makes it easier to display a list of database records retrieved from a database through an Ajax call. data linking makes it easier to keep the data and user interface in sync for update scenarios.

Currently, we are working on an extension of the Data linking proposal to supportDeclarativeData linking. We want to make it easy to take advantage of data linking when using a template to display data.

For example, imagine that you are using the following template to display an array of product objects:

Notice the {link name }}and {link price} expressions. these expressions enable declarative data linking between the span elements and properties of the product objects. the current jquery templating prototype supports extending its syntax with custom template commands. in this case, we are extending the default templating syntax with a custom template command named "Link ".

The benefit of using data linking with the above template is that the span elements will be automatically updated whenever the underlying "product" data is updated. declarative data linking also makes it easier to create edit and insert forms. for example, you can create a form for editing a product by using declarative data linking like this:

Whenever you change the value of the input elements in a template that uses declarative data linking, the underlying JavaScript data object is automatically updated. instead of needing to write code to scrape the HTML form to get updated values, you can instead work with the underlying data directly-making your client-side code much cleaner and simpler.

Downloading working code examples of the above scenarios

You can download this. ZIP file to get with working code examples of the above scenarios. The. ZIP file except des 4 static html page:

    • Listingtemplating.htm-registrstrates basic templating.
    • Listing2_templatingconditionals.htm-extends strates templating with the use of the IF and each template commands.
    • Listing3_datalinking.htm-illustrates data linking.
    • Listing4_converters.htm-wide strates using a converter with data linking.

You can un-zip the file to the file-system and then run each page to see the concepts in action.

Summary

We are excited to be able to begin particle ating within the open-source jquery project. we 've got Ed lots of encouraging feedback in response to our first two proposals, and we will continue to actively contribute going forward. these features will hopefully make it easier for all developers (including ASP. NET developers) to build great Ajax applications.

Hope this helps,
Jquery template and Data Binding DEMO code

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.