Tales from the Evil Empire

Bertrand Le Roy's blog

News


Bertrand Le Roy


Add to Technorati Favorites Tales from the Evil Empire - Blogged

Blogs I read

My other stuff

Archives

Creating jQuery plug-ins from MicrosoftAjax components

(c) Bertrand Le Roy We had an interesting discussion recently on the ASP Insiders mailing list and ended up talking about what cool stuff we could build on top of jQuery. Many interesting things were mentioned and it was a very useful discussion but one suggestion in particular struck my curiosity as it was something I had investigated before and that could be improved on with very little code.

I had already written a little plugin to enable instantiation of Microsoft Ajax components on the results of a jQuery selector:

jQuery.fn.create = function(type, properties) {
    return this.each(function() {
        Sys.Component.create(type, properties, {}, {}, this);
    });
};

I have another version that is a little more elaborate and takes a bag of properties and events instead of just properties (get it from the attached sample) but you get the idea. This makes it fairly easy to instantiate components based on a selector:

$(":text.nomorethanfive")
    .create(Bleroy.Sample.CharCount, { maxLength: 5 });

But if this were a native jQuery plugin instead of a Microsoft Ajax component, as Rick Strahl suggested, chances are you’d do something like this instead:

$(":text.nomorethanfive").charCount({ maxLength: 5 });

We can actually get that exact result fairly easily by writing a plugin that is specific to this component, but we can’t expect every Microsoft Ajax component author to also write a jQuery plugin, can we? We can do better than that. Here is a second small piece of code that is still generic but is in the business of creating jQuery plugins:

Sys.Component.exposeTojQuery = function(type, pluginName) {
    return jQuery.fn[pluginName] = function(properties) {
        return this.each(function() {
            Sys.Component.create(type, properties, {}, {}, this);
        });
    }
}

Like above, the version in the sample is better than that in that it supports events in addition to properties but again you get the idea. Thanks to this function, we can create a jQuery plugin for any existing Microsoft Ajax component with a single line of code:

Sys.Component.exposeTojQuery(Bleroy.Sample.CharCount, "charCount");

After this call, we can write exactly the code we wrote before, but without having had to write a specific plugin. We can then use charCount like a native jQuery plugin:

$(":text.nomorethanfive").charCount({ maxLength: 5 });

This is really easy to use, and I quite like it. We could for example add the following line of code to the new MicrosoftAjaxTemplates.js:

if (jQuery)
Sys.Component.exposeTojQuery(Sys.UI.DataView, "dataView");

This would enable us to instantiate DataView controls as simply as this:

<ul class="dv">
    <li>{{ $dataItem }}</li>
</ul>
<script type="text/javascript">
    $(".dv").dataView({ data: ["foo", "bar", "baz"] });
</script>

Get the code from here (you need to include jquery-1.3.2.js to the script folder):
http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip

Comments

Chris Bower said:

Thanks Bertrand, this will make the client web applications easier to design.

# May 5, 2009 3:45 AM

MisterFantastic said:

Hi Sir,

The article is very nice but the starting is little bit confusing. Thanks for letting us know this.

Thanks,

Thani

# May 5, 2009 7:10 AM

Aaron Powell said:

Hey Bertrand, nice post, I decided to take your concept and have it do all of a certain base type. Just a POC and something for a bit of fun :P

www.aaron-powell.com/.../creating-jquery-plugins-for-ms-ajax-components,-dynamically!.aspx

# May 5, 2009 8:33 AM

WHY ??? said:

WHY ???WHY ???WHY ???WHY ???WHY ???

# May 5, 2009 9:18 AM

WHY 2 said:

I haven't found a good reason yet to mix the 2.  I have found using jquery without the .net ajax so much better and jquery can process web services just like .net ajax.  The .net ajax is so bloated. This site is the best option I feel.

encosia.com/.../using-jquery-to-consume-aspnet-json-web-services

# May 5, 2009 4:11 PM

Bertrand Le Roy said:

Globalization, client templates, client data and live bindings, integration with ADO.NET Data Services, client and server-side support for the back button, a client component model on which third parties are building professional & supported components (Telerik, Infragistics, ComponentArt, etc.), etc. What's bloat to you is useful to others. Client library is 24kB gzipped (jQuery is 18kB), and in 4.0 you can reduce it down to 7kB depending on what pieces you need.

# May 5, 2009 4:27 PM

Andrew Webb said:

For us, globalization is sooo important.  Every date and number that is displayed to the user must be formatted according to the browser's preferred language.  Some of this formatting we do in web methods, but some formatting must be done client-side.  The MS AJAX 3.5 library is so good at this (although naturally it doesn't have as full a range of formatting options as the .NET Framework itself).

Don't know how the 4.0 library will do it, but I've written up how to enable script globalization in MS AJAX 3.5 for ASP.NET MVC apps here:-

stackoverflow.com/.../what-about-script-globalization-of-microsoftajax-js-in-asp-net-mvc

# May 6, 2009 2:22 AM

ewschone said:

If the globalization etc is indeed so good, then why not contribute to jquery ?

# May 6, 2009 9:08 AM

bcardiff said:

Just nice. Between this and the my previous work on <a href="weblogs.manas.com.ar/.../">Declarative jQuery (with Microsoft Ajax)</a> we are able to go from one side to the other :-) letting users of either libraries use the components of the other one with less effort.

# May 6, 2009 9:13 AM

Karl said:

Do you guys set aside a day of the week to justify the work you do? Globalization? Is that how people are dong it these days? Via javascript? Huh...

Data Services return JSON, why do you need msAjax for that?

Using jQuery you can build your own plugin quicker than you can download those "professional" third-party components. Or you know, you can just goto plugins.jquery.com

It isn't about size or bloat, but about continuing to put effort into outdated and inferior solutions (and dragging countless developers down with you). Its about streamlining and adapting.

# May 6, 2009 9:34 AM

Bertrand Le Roy said:

@Karl: if you're going to build a real client application that uses the browser as more than a dumb terminal, you're going to handle data on the client. On the wire, the data should be culture-neutral, which means that the client needs strong globalization capabilities. The built-in JavaScript support is just lame and US-centric. We had a long thread on Dave Ward's post on date handling.

For data services, sure they are JSON, but that's beside the point. With Microsoft Ajax, you can just point at a data service and poof, you have automatic change tracking on the client and can send those back by just calling save. It doesn't get any easier, which some people value.

Using jQuery *you* can build your own plugin faster. Good for you. About plugins.jquery.com, there are some real gems over there, I'm not the one who's going to say otherwise. But there is also a lot of junk, and it's mostly unsupported and unsupportable. Discovering the good ones takes time (despite the rating system: the organization of the catalog is showing signs of weakness under the load) and not everyone can afford that. Believe it or not, there are people who prefer to pay for a guaranteed level of quality and support. I would tend to be more on the side of exploring, building and using jQuery plugins myself for many things because that's the way my brain is wired but I know for a fact that this is not representative of the whole user base for ASP.NET.

So for *you* it's not about bloat and size (although you just said "using jQuery you can build your own plugin quicker than you can download those 'professional' third-party components" so apparently it is, at least a little bit). Apparently, for the very brave "WHY 2" it is: "the .net ajax is so bloated". So it's about streamlining. Sure. You streamline your applications the way you want (with jQuery), we streamline our framework and provide cool new features for our users to streamline their own applications. At least we try. Adapting? We adopted jQuery instead of building our own. How's that for adapting?

I'm not sure what you're trying to say here. Sometimes it seems like you would only be satisfied if Microsoft stopped all software development. I'm afraid this is not going to happen.

@Ewschone: that's an interesting idea, but it's actually already done, in a way. The Microsoft Ajax library contains all globalization files, which are perfectly fine to use with any other library. In 4.0, we also split our library into much smaller pieces so that you can pick just the pieces that you need. I'm not sure it's very interesting to tie those pieces onto the jQuery object (it's actually a few lines of code but what's the value really?). jQuery is mainly about DOM manipulation. Globalization is a little orthogonal to that. I'm really not sure there's much value in grafting globalization on it. But I'm absolutely open to any arguments you may have and as we're precisely exploring ways we can contribute to the jQuery ecosystem I'm really willing to listen to what you'd have to say about that.

# May 6, 2009 1:32 PM

Hamza said:

Really great stuff here. Nice work guys but can i know when this will ship or be mature enough to be used in production. I really want to use it in my next project.

# May 6, 2009 1:56 PM

Bertrand Le Roy said:

@Hamza: I'm really just experimenting here, but we're talking about four lines of code here. This shouldn't be too risky to use in your next project. Just ping me if you have any problem with it.

# May 6, 2009 2:14 PM

Andrew Webb said:

It's important to bear in mind that not everyone uses jQuery.  We started off with the intention to use it, but ended up with ExtJs for our current web app because of its far more mature widgets (check out the grid), which you can find all in one place.  No hunting around the plugin-o-sphere.

ExtJs has 45 localization files, but they mainly contain localized resource strings for the widgets.  There's limited support for formatting a date for display, and no support for formatting a floating point number (AFAICS).  Compare this with MS AJAX's globalization files, which give you great support for formatting and parsing dates and numbers in 205 different cultures.  Most of our formatting for display takes place on the server, but occasionally we need the client-side globalization support (e.g. for formatting numbers that the user enters into the editable ExtJs grid).

So if Bertrand and team are going to keep globalization (and other useful parts of MS AJAX) separate from jQuery, and applicable to both jQuery and other JavaScript libraries, I can only applaud.

# May 7, 2009 2:18 AM

Oskar Austegard said:

#twitcode version of Bertrand's exposeTojQuery function:

c=Sys.Component;c.$it=function(t,pn){return jQuery.fn[pn]=function(p){return this.each(function(){c.create(t,p,{},{},this);});}}

;-)

# June 8, 2009 10:47 AM

Bertrand Le Roy said:

@Oskar: awesome, but you should add var in front of that c to avoid a warning in strict mode.

# June 8, 2009 4:51 PM

Edward said:

@Bertrand - re Karl Seguin's comments. I totally get where you're coming from on this. With regards to Karl, I'm afraid he's just jumped on the Scott Bellware Not-Invented-By-Microsoft bandwagon that's so vogue in certain cliques. The problem is, whilst he tries to ride the smarter than thou high horse for not using the MS toolchain, he's nowhere near as talented or as interesting as Scott, who's a real pragmatist.

# January 29, 2010 6:12 PM