JQuery integration

Published Mon, Oct 26 2009 11:08

One of the cool features MS AJAX preview 6 introduces is better JQuery integration. From now on, MS AJAX components (which have been defined through a script info object registered through a defineScript(s) method call) are exposed as jQuery plugins. This means that you can write something like this:

<head runat="server">
  <style type="text/css">
      .sys-template{
          display:none;
      }
  </style>
  <script src="Scripts/MicrosoftAjax/start.debug.js" 
type="text/javascript">
</
script> <script type="text/javascript"> Sys.require([Sys.components.dataView,
Sys.scripts.jQuery]); var items = [ { name: "luis", address: "fx" }, { name: "john", address: "lx" }, { name: "rita", address: "fx" } ]; Sys.onReady(function () { $("#myDv").dataView({
data: items,
itemTemplate: "#template" }) .css("background-color", "lightgray"); }); </script> </head> <body> <div id="myDv"> </div> <div id="template" class="sys-template"> <div>{{name}}</div> </div> </body>

Pretty cool, right? Instead of using the traditional MS AJAX Sys.create helper methods, we’re using a JQuery object for wrapping the div so that we can attach it to a MS AJAX DataView control and set its background color.

Notice that the dataView method (added to the jQuery.fn object) is built on the fly when the JQuery script is listed on the depedencies list passed to the Sys.require method. This is one of those small details that makes life better for everyone…Stay tuned for more.

Filed under: ,

Comments

# Dave Reed said on Monday, October 26, 2009 7:17 AM

You can also just include jquery the way you normally do, FYI.

# luisabreu said on Monday, October 26, 2009 7:21 AM

Yes, but I'm really lazy and Sys.require seems to require few key pushes...

# Dave Reed said on Monday, October 26, 2009 8:08 AM

Yup -- some people will probably prefer to do it the usual way, just wanted to point out it isn't a requirement that you load jquery through the loader. It plays nice.

This gives you a pretty easy way to load jquery dynamically along with your own scripts, fyi.. so start.js can be useful to you even if you don't use the ms ajax framework proper.

# luisabreu said on Monday, October 26, 2009 8:24 AM

Agreed...

# william apken said on Monday, October 26, 2009 9:17 AM

If you are planning to implement the dataview as a component that contains several controls, ie. a dataGrid.

by not using like Dave said the proper framework. Do we lose the ability to dispose of the component and all controls, behaviors and handlers that are contained with the component?  or even they ability .find() the component?

can you still do:

myDV = $("#myDv").dataView({

                          data: items,

                          itemTemplate: "#template" })

                   .css("background-color", "lightgray");

       });

# luisabreu said on Monday, October 26, 2009 10:18 AM

I think that what Dave was saying is that even if you don't want to use MS AJAX components, you can rely on start.js file for having on-demand loading of the script files you're using.

# uberVU - social comments said on Tuesday, October 27, 2009 8:07 PM

This post was mentioned on Twitter by luisabreu: Just blogged: JQuery integration: One of the cool features MS AJAX preview 6 introduces is better .. http://bit.ly/1VSH2N

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover