<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sam Mueller &#187; jQuery</title>
	<atom:link href="http://samuelmueller.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://samuelmueller.com</link>
	<description>APPLICATION DEVELOPER EXTRAORDINAIRE</description>
	<lastBuildDate>Thu, 24 Nov 2011 04:23:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Google Closure Templates with ASP.NET MVC in Visual Studio 2010</title>
		<link>http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/</link>
		<comments>http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 18:40:30 +0000</pubDate>
		<dc:creator>Sam Mueller</dc:creator>
				<category><![CDATA[Client Templates]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Google Closure Templates]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://samuelmueller.com/?p=119</guid>
		<description><![CDATA[Client-side templates have become a vital component of AJAX-driven websites. The web is undoubtedly trending towards sites that load content dynamically after the page loads, rather than all at once in the initial page request. This pattern allows web pages &#8230; <a href="http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Client-side templates have become a vital component of AJAX-driven websites.  The web is undoubtedly trending towards sites that load content dynamically <em>after</em> the page loads, rather than all at once in the initial page request.  This pattern allows web pages to become lighter and more responsive, which translates to a better experience for the user.  However, the conventional server side templating and databinding techniques that web developers typically use aren&#8217;t as effective anymore.  That is why there are so many JavaScript template solutions that have popped up in recent years:</p>
<ul>
<li><a href="http://aspnet.codeplex.com/wikipage?title=AJAX&amp;referringTitle=Home">ASP.NET Ajax 4</a></li>
<li><a href="http://www.extjs.com/deploy/dev/docs/">ExtJs and XTemplate</a></li>
<li>jQuery Plugins like <a href="http://github.com/raid-ox/chain.js">Chain.js</a>, <a href="http://jtemplates.tpython.com/">jTemplates</a>, <a href="http://arashkarimzadeh.com/index.php/jquery/11-jbind-jquery-bind-template.html">jBind</a>, etc.</li>
<li><a href="http://code.google.com/p/closure-templates/">Google Closure Templates</a></li>
</ul>
<p>Google&#8217;s Closure Templates is the new kid on the block, and the subject of this article.  One might wonder: Why do we need yet another JavaScript templating solution?  The main advantage that sets Closure Templates apart from the other libraries is the included compiler.  Other templating solutions either parse a string of special template syntax, or traverse through actual html elements with special markup or extra classes.  When making light use of client-side templates, these solutions can work very well.  But, as you start to build applications with extremely large datasets and complex templating, then performance starts to become an issue.  These scenarios are where Closure templates begin to shine.  Instead of directly using the template that you write, you run it through the compiler to output JavaScript functions that you can use in your code.  This process is advantageous on two levels; the first is that the JavaScript functions outputted from the compiler are going to be optimized and extremely fast.  Secondly, the compiler can also create server side compatible templates as well, so that you can write your templates once and be able to use them either in JavaScript or in server-side code.  Unfortunately, the current version of the compiler can only generate Java code, and there is no option for .NET languages such as C#.  This is a bummer for .NET developers, but even without C# templates, there is still great value in lightning-fast, compiled JavaScript templates.</p>
<h2>Creating your first Closure Template in Visual Studio 2010</h2>
<p>Let&#8217;s start with a new ASP.NET MVC Project:</p>
<p><img class="alignnone size-full wp-image-131" title="vs2010-new-mvc-project" src="http://samuelmueller.com/wp-content/uploads/2009/12/vs2010-new-mvc-project.png" alt="vs2010-new-mvc-project" width="816" height="483" /></p>
<p>You can create a Web Application project if you are more comfortable with that type of project structure.  Because we will be dealing with just JavaScript, it shouldn&#8217;t matter.</p>
<p>After creating a project, download the <a href="http://code.google.com/p/closure-templates/">compiler and JavaScript utility library</a>.  Extract the zip file, and copy both SoyToJsSrcCompiler.jar and soyutils.js to your templates directory:</p>
<p><img class="alignnone size-full wp-image-137" title="soy-compiler" src="http://samuelmueller.com/wp-content/uploads/2009/12/soy-compiler.png" alt="soy-compiler" width="327" height="522" /></p>
<p>You will see in the screenshot above that I have changed the Build Action of SoyToJsSrcCompiler.jar to &#8220;None&#8221;, and Copy to Output Directory is set to &#8220;Do not copy&#8221;.  This ensures that the compiler is not compiled into the dll, and the file is not copied to the output folder.  The latter is especially useful when using Visual Studio&#8217;s publish feature, because the compiler is not necessary when deploying your website.</p>
<p>Now that we have the required files in place, let&#8217;s go ahead and create a soy template file, example.soy.  This is the file that will contain one or more templates using Closure&#8217;s template syntax.  After creating a template in this file, we will then use the compiler to generate a JavaScript representation of the template that we will reference in our html page.  Every soy file should have the three following components, in the following order:</p>
<ul>
<li>A namespace declaration.</li>
<li>One or more template definitions.</li>
<li>A newline at the end of the file.</li>
</ul>
<p>Go ahead and enter the following example template in your example.soy file:<br />
<code><br />
{namespace closure.examples}</code></p>
<p><code> </code></p>
<p><code>/**<br />
* Says hello to a person.<br />
* @param name The name of the person to say hello to.<br />
*/<br />
{template .helloName}<br />
Hello {$name}!<br />
{/template}</p>
<p></code></p>
<p>Make sure that your soy file is encoded as ANSI, rather than UTF-8.  Even though Google says <a href="http://code.google.com/closure/templates/docs/concepts.html">UTF-8 should be supported</a>, in Windows 7 x64 (and maybe other Windows operating systems and versions) this is currently not the case.  And if you create a file within Visual Studio, it will encode the file as UTF-8 and you will get the following exception when compiling a template:</p>
<p><code>Exception in thread "main" com.google.template.soy.base.SoySyntaxException: In file example.soy: Tag 'namespace' not at start of line.</code></p>
<p><img class="alignnone size-full wp-image-155" title="closure-compiler-encoding-exception" src="http://samuelmueller.com/wp-content/uploads/2009/12/closure-compiler-encoding-exception.png" alt="closure-compiler-encoding-exception" width="858" height="196" /></p>
<p>If you are unsure that your file is saved with the proper encoding, just open it up in notepad, and select File &gt; Save As to see or change the encoding.</p>
<p>After we&#8217;ve included the necessary files in our project, it&#8217;s time to make compiling templates a little more user-friendly.  The jar file is pretty annoying to call from the command line and manually change the parameters for each of your different soy files.  Visual Studio has a perfect solution for this, and it&#8217;s called <em>External Tools</em>.  This feature allows you to set up a VS menu item and seamlessly run commands from the IDE with just the press of a button.  To do this, first click on Tools &gt; External Tools from the menu:</p>
<p><img class="alignnone size-full wp-image-141" title="external-tools" src="http://samuelmueller.com/wp-content/uploads/2009/12/external-tools.png" alt="external-tools" width="348" height="374" /><br />
<code><br />
From here, you can create a new menu item with the following parameters:<br />
Title: <strong>Compile Closure Template</strong><br />
Command: <strong>C:Program Files (x86)Javajre6binjava.exe</strong><br />
Arguments: <strong>-jar "$(ItemDir)SoyToJsSrcCompiler.jar" --outputPathFormat $(ItemFileName).js $(ItemFileName)$(ItemExt)</strong><br />
Initial Directory: <strong>$(ItemDir)</strong><br />
</code><br />
(Thanks to Tj Stewart for this tip)</p>
<p>Note that your Command entry may differ, depending on where your java installation resides on your computer.  Here is what it looks like:</p>
<p><img class="alignnone size-full wp-image-150" title="closure-template-tool" src="http://samuelmueller.com/wp-content/uploads/2009/12/closure-template-tool.png" alt="closure-template-tool" width="471" height="460" /></p>
<p>Once you have this setup, compiling your soy templates is as simple as selecting the soy file in solution explorer and pressing the &#8220;Compile Closure Template&#8221; button in the Tools menu:</p>
<p><img class="alignnone size-full wp-image-145" title="invoke-closure-template-tool" src="http://samuelmueller.com/wp-content/uploads/2009/12/invoke-closure-template-tool.png" alt="invoke-closure-template-tool" width="348" height="349" /></p>
<p>When you attempt to compile a template, make sure to have your output window open.  If there are any exceptions that occur during the compilation, this is where they will be displayed.  If the compilation was successful, you should now have a new example.js file in the Templates directory.  Note that it won&#8217;t be added to the project automatically, so you&#8217;ll have to add it yourself.  Subsequent compiles for the same soy file will only require this step once.</p>
<p>Now let&#8217;s create a page to utilize the new template, and call it example.html.  We could create an MVC view, but it really isn&#8217;t necessary since we are only dealing with JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;Scripts/Templates/soyutils.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;Scripts/Templates/example.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		$(function () {
			$('#wrapper').html(closure.examples.helloName({ name: 'Sam' }));
		});
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I&#8217;m using a little jQuery to make my life easier here, and notice that I am passing JSON with the Closure template parameters as the properties of the object.  Your result should look something like this:</p>
<p><img src="http://samuelmueller.com/wp-content/uploads/2009/12/closure-result.png" alt="closure-result" title="closure-result" width="638" height="236" class="alignnone size-full wp-image-164" /></p>
<p>In summary, Google Closure Templates provide a scalable solution to client side templating by providing an understandable syntax in the form of soy files, with the ability to compile those templates into fast and efficient JavaScript functions that you can use in your pages.  Even though there is no current support for reusing Closure templates in C#, there is still value in utilizing this solution in a Visual Studio project, especially when the External Tools feature makes compilations so easy and convenient.</p>
<p>I hope this article serves as a starting point to getting up and running in Visual Studio 2010.  In future articles, I will be delving into more complex examples and explanations of Closure Templates in real-world scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>DynamicLoader Plugin &#8211; Dynamically Loading ASP.NET User Controls with jQuery</title>
		<link>http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery/</link>
		<comments>http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 06:59:53 +0000</pubDate>
		<dc:creator>Sam Mueller</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[DynamicLoader Plugin]]></category>
		<category><![CDATA[User Controls]]></category>

		<guid isPermaLink="false">http://blog.samuelmueller.com/?p=33</guid>
		<description><![CDATA[Live Demo &#124; Download Sample Solution (52.77 kb) ASP.NET User Controls are pretty useful. They allow functional modules of code and markup to be encapsulated in such a way that reuse is convenient and easy, without sacrificing the power or &#8230; <a href="http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://demos.samuelmueller.com/dynamicloader/">Live Demo</a> | <a href="/wp-content/uploads/2009/11/DynamicLoader.zip">Download Sample Solution (52.77 kb)</a></p>
<p>ASP.NET User Controls are pretty useful.  They allow functional modules of code and markup to be encapsulated in such a way that reuse is convenient and easy, without sacrificing the power or integration of the ASP.NET model.  As we move into an era of AJAX-driven websites, this modularity is still very important. Can the user controls that we all know and (mostly) love still help with this encapsulation, despite being engineered before AJAX techniques emerged?  I think they can.  But at this point in the ASP.NET timeline, user controls are in need of some help.</p>
<p><strong>The Fundamental Problem</strong></p>
<p>With AJAX, more and more content is being dynamically loaded by the client on demand, rather than being included in the original http response.  This fundamental change conflicts with the user control&#8217;s current usage model of being attached to the control heirarchy during the page lifecycle on the server&#8211;either through markup, or using the Page.LoadControl method in code.  For user controls to be useful in the world of AJAX and demand loading, we would need to find a way to load them outside of the normal page lifecycle, and use javascript to get the the rendered HTML and inject it into our page.  Luckily, this isn&#8217;t too difficult to accomplish.</p>
<p>The following example illustrates a basic scenario in which we have a page that uses jQuery to load a user control when a button is clicked.  The calling page is pretty simple:</p>
<p><img src="/wp-content/uploads/2009/11/jquery-load-user-control.png" alt="jquery-load-user-control" title="jquery-load-user-control" width="723" height="339" class="alignnone size-full wp-image-37" /></p>
<p>As you can see, all I&#8217;ve done in jQuery&#8217;s ready event handler is wire up the click event of the button to make an ajax call to a web service.  The data result that is returned from the ajax call is then added into the content div on the page.  Let&#8217;s take a look at the web service that we are calling in that code:</p>
<p><img src="/wp-content/uploads/2009/11/renderuc-svc-operation.png" alt="renderuc-svc-operation" title="renderuc-svc-operation" width="830" height="289" class="alignnone size-full wp-image-38" /></p>
<p>This is a pretty standard WCF Ajax service, which uses a utility class called UserControlUtility by calling its RenderAsString method, which looks like this:</p>
<p><img src="/wp-content/uploads/2009/11/renderuc-utility.png" alt="renderuc-utility" title="renderuc-utility" width="773" height="323" class="alignnone size-full wp-image-39" /></p>
<p>In the helper method above, I&#8217;m simply accepting a parameter called path, which allows us to use the LoadControl method in the usual way.  If you are worried about the potential baggage of instantiating a Page object for every User Control that is rendered, don&#8217;t lose too much sleep over it.  A page object that is instantiated like this is pretty lightweight, and doesn&#8217;t go through the heavy ASP.NET Page lifecycle that occurs on a normal page load.</p>
<p>This is pretty nifty for simple scenarios, but big challenges arise when the application gets more complicated.  What happens when the user control has javascript of it&#8217;s own?  Well ordinarily you would have a few options.  One option that I defaulted to when starting out with jQuery was to write all the JavaScript in the calling page, and just apply it to the user control&#8217;s html when it has been loaded.  This is not the best solution, because you lose the encapsulation that we were trying to maintain with user controls in the first place.  The second solution is to include the javascript within the user control within another jQuery ready handler.  This works out much better, because the client functionality gets to be bundled with the markup for clean encapsulation.  Additionally, the included javascript will be excuted when the control is rendered on the parent page, thanks to jQuery.  But has this solved all of our problems?  Not quite.</p>
<p><strong>Mo Javascript, Mo Problems</strong></p>
<p>To illustrate how problems can arise with that last solution, let me give an example. Say you are developing a real-time stock-screening application.  In this application, you have a user control called StockItemRow.ascx that had quite a bit of javascript associated with it. You also have a page called Screener.aspx that periodically polls a web service for matching stocks, and adds those stocks to the grid via a rendered instance of StockItemRow.ascx.  And suppose the user control had a good deal of javascript bundled with it, and also a few nested user controls of its own (with their own javascript, of course).  What were to happen if you dynamically added 50 or 60 rows over a few minutes? You may see what I am trying to get at here.</p>
<p>The problem is that the JavaScript is being loaded over and over on each successful new request for data, simply because it is bundled inside the rendered user control.  As you load more and more data onto the page, this becomes a bigger and bigger waste.  Plus, unless you write your javascript very carefully, each new dynamically loaded user control could end up applying it&#8217;s javascript to other user controls that have already been loaded.  Yuck!  In order to solve these problems, it is going to take a little more work.</p>
<p>The first issue we need to solve is the repititious loading of unnecessary javascript.  To do this, we need to separate it out from the user control into it&#8217;s own js file.  Some may argue that we are losing encapsulation here, but I disagree.  I think just if an aspx page can have both a file for markup and a codebehind file, then a user control can have both a markup file and a js file (and it&#8217;s codebehind file, for that matter). After we have separated it out, we have freed ourselves to be able to load the javascript file once, while still rendering the user control multiple times.</p>
<p>But just separating the javascript out doesn&#8217;t solve our problems. We need to somehow &#8220;register&#8221; a single instance of javascript on the page, and have any dynamically loaded user controls use just that instance.  Additionally, we need to make sure that the javascript is capable of being applied to individual user controls, without affecting other user controls that have already been wired up and loaded on the page. </p>
<p><strong><br />
Enter jQuery.DynamicLoader</strong></p>
<p>jQuery.DynamicLoader is a simple jQuery plugin I wrote that allowed a parent page to dynamically load User Controls and their corresponding script files on demand. Here is the way it works:</p>
<ul>
<li>You reference jQuery.DynamicLoader on your parent page.</li>
<li>
Create an ajax service that renders user controls, similar to the example I showed earlier</li>
<li>Anytime you want to load a user control on that page, call $.dynamicLoader.loadUC() with the appropriate options.  This will fetch the rendered user control, and its corresponding javascript file. If the javascript is being loaded for the first time, DynamicLoader will register that instance as the singleton for all subsequent user controls of that same type.</li>
<li>The javascript instance is then invoked with the rendered user control as its UI context.</li>
</ul>
<p>Let&#8217;s jump into the sample project I&#8217;ve created as an example:</p>
<p><a href="/wp-content/uploads/2009/11/DynamicLoader.zip">DynamicLoader (52.77 kb)</a></p>
<p>The project contains a single page Default.aspx, and two user controls, TableWidget.ascx and CellWidget.ascx.   The purpose of the project is to demonstrate a page intitally with no content, and how we can dynamically load several tiers of user controls, each with their own scripts.  We start from a single button on Default.aspx that will dynamically load a new TableWidget every time it is clicked.  Inside each TableWidget is a button gets wired up to load its own user controls, this time CellWidgets.  Each CellWidget has its own javascript that needs to execute as well. </p>
<p> Here is how the first button is wired up with jQuery: </p>
<p><img src="/wp-content/uploads/2009/11/invoke-dynamic-loader.png" alt="invoke-dynamic-loader" title="invoke-dynamic-loader" width="761" height="474" class="alignnone size-full wp-image-36" /></p>
<p>As you can see, it is calling DynamicLoader&#8217;s loadUC function, which takes a few options: ucName is the path to the user control to be loaded, queryString allows you to pass parameters to your UserControl to help render it on the server, and eventBindings allows you to handle events that are fired within the usercontrol.</p>
<p>As I mentioned earlier, the javascript in your user control needs to be registered before it can be used.  Don&#8217;t get scared off now, it&#8217;s only two extra lines of code:</p>
<p><img src="/wp-content/uploads/2009/11/dynamic-loader-compatible-script.png" alt="dynamic-loader-compatible-script" title="dynamic-loader-compatible-script" width="742" height="363" class="alignnone size-full wp-image-34" /></p>
<p>We have a standard jQuery ready handler, and inside that we call DynamicLoader&#8217;s registerUC function.  This will only be loaded once, even if multiple TableWidgets are loaded afterwards.  Also notice the event triggers.  You can create as many different types of events as your heart&#8217;s desire, as long as the parent knows the name of the event (and references it in the eventBindings option).  I&#8217;ve included ready, busy, unbusy, and finished in the default options.  The ready event is one that I consider critical, because it is the event that the parent will use to attach the user control to the page.</p>
<p>Here is a screenshot of the demo:</p>
<p><img src="/wp-content/uploads/2009/11/dynamic-loader-demo.png" alt="dynamic-loader-demo" title="dynamic-loader-demo" width="691" height="453" class="alignnone size-full wp-image-35" /></p>
<p><a href="http://demos.samuelmueller.com/dynamicloader/">Live Demo</a> | <a href="/wp-content/uploads/2009/11/DynamicLoader.zip">DynamicLoader.zip (52.77 kb)</a></p>
<p>You can see that there are buttons on the CellWidget that do some trivial javascript actions, and also a button that demonstrates an event being monitored by the parent user control.</p>
<p><strong>Room for Improvement</strong></p>
<p>DynamicLoader is more of a proof concept than a full-fledged plugin, and there are several areas in which it needs to be improved:</p>
<p>The event chaining needs some work.  I haven&#8217;t really tested it with events that bubble more than two layers up.<br />
Right now it doesn&#8217;t look like jQuery&#8217;s $.getScript is caching the scripts.  I&#8217;d like to rewrite a version of getScript that does.<br />
The registration system is very rigid at this point.  It expects you to pass in a user control&#8217;s path, and the script needs to register itself with that exact path as its key (without the extension).<br />
So there you have it. This technique allows you to treat your User Controls as neatly encapsulated modules that are loaded and configured on demand.  Plus, there is no limit to nesting your user controls, and they will load efficiently and within their own context.   Finally, you don&#8217;t have to break communication with your user controls.  The event binding allows a separation of concerns, while still being able to act on important things that happen within the user control.</p>
<p>I hope you find this technique useful, and please let me know if you have suggestions or improvements!</p>
]]></content:encoded>
			<wfw:commentRss>http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

