UPDATE: This code is obsolete. Check out development of WebScriptFX for the availability of a reusable library for jQuery 1.4!
I have been very impressed with Script# since I started using it on a Silverlight and Microsoft Web Messenger Toolkit integration project, but one thing I was immediately missing was jQuery. I have really only started using jQuery since the most recent version (1.3.2) but it has already saved me a ton of time on multiple projects. Although it took some snooping and creative problem solving, I was happy to find that it was possible to get nearly the same experience using the jQuery functions from Script#.
I hope to post some more in the near future on how to use the functionality, but it consists of:
- jQuery.cs – imports of all of the static and instance methods
- jQueryFunctions.cs – provides the ability to create handler functions and access arguments
- jQueryInterfaces.cs – provides import of jQuery objects and constants used by the library
To get started quickly, you can download the whole jQuery 1.3 Import for Script# and add the files to any Script# project. Add using statements for jQuery, jQueryFunctions and jQueryInterfaces and then use Global.jQuery to run queries. The static methods are available through the static class fn. Compile your scripts and add them to a page that already has the jQuery scripts referenced and you should be good to go. See the FunctionContext class for static methods that allow you to treat Script# static methods as script handlers (NOTE: they run in the global scope).
It has not been extensively tested but I have been using it to run document ready handlers, lookup and manipulate elements and have not run into any issues yet. I had to work around some design restrictions of Script# and hope to also write some posts on how I went about implementing this. Please let me know if you have any issues, suggestions or if you are able to use this to make your life easier.
I know I have been, and hope to add jQuery UI support soon.
jQuery for Script#: Document Ready Handlers
UPDATE: This code is obsolete. Check out development of WebScriptFX for the availability of a reusable library for jQuery 1.4!
This is the first example of how to use the jQuery 1.3 Import for Script#. A common task in jQuery is waiting until the DOM is ready before running some script, such as attaching event handlers to elements. This example works simply by attaching the document ready handler within a Script# static constructor, which runs as soon as the class is created in your compiled JavaScript.
using System; using jQuery; using jQueryFunctions; public static class MyClass { static MyClass() { Global.jQuery(FunctionContext.CreateEmpty(typeof(MyClass), "documentReady")); } public static void DocumentReady() { Script.Alert("The document is ready!"); } }FunctionContext.CreateEmpty creates a wrapper around MyClass.DocumentReady, a handler function with no parameters. Its then passed to the jQuery overload that accepts a Function. This compiled Script# method will be called “documentReady”. Handlers must always be static and classes may have namespaces. The wrapper is created internally via the Function constructor.