<?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>BBQIguana &#187; VB.net</title>
	<atom:link href="http://www.bbqiguana.com/tag/vb-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bbqiguana.com</link>
	<description></description>
	<lastBuildDate>Fri, 28 Oct 2011 13:17:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Recursive script inclusion with REQUIRE</title>
		<link>http://www.bbqiguana.com/2009/10/recursive-script-inclusion-with-require/</link>
		<comments>http://www.bbqiguana.com/2009/10/recursive-script-inclusion-with-require/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 19:16:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[VB.net]]></category>

		<guid isPermaLink="false">http://www.bbqiguana.com/?p=132</guid>
		<description><![CDATA[One thing that tends to seriously limit the "best practices" when it comes to enterprise development and code reuse for Javascript is a lack of INCLUDE or REQUIRE functionality.]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:5px;" class="tweet_button"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.bbqiguana.com/2009/10/recursive-script-inclusion-with-require/" data-text="Recursive script inclusion with REQUIRE" data-count="vertical" data-via="bbqiguana"">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><p>One thing that tends to seriously limit the &#8220;best practices&#8221; when it comes to enterprise development and code reuse for Javascript is a lack of INCLUDE or REQUIRE functionality.</p>
<p>Code reuse and portability requires various functionality to be compartmentalized into individual .js files containing only related functionality, but on the web, including several files means making lots of unnecessary roundtrips, which can add up to a huge performance cost in the load time of your pages.</p>
<p>Good compartmentalized code also necessitates some sort of REQUIRE mechanism, which allows one unit of code (ie, script file) to specify when it won&#8217;t run properly without another unit of code.</p>
<p>Thus, while working on a VB.Net project, I created this simple Javascript loader, which looks at the name of the directory in which an app resides, and then looks for a javascript file by that name and loads it.  Directives can be placed in that Javascript file (and in any subsequent file) requiring the inclusion of additional script files.</p>
<p>Not only does this allow for easier inclusion of libraries as needed, but it also makes it easier to strip out unused code if/when it is no longer needed by an app.</p>
<p>This source code is also available in a <a href="/2009/10/recursive-script-inclusion-with-require-c/">C# version</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">IO</span>
&nbsp;
Partial <span style="color: #0600FF;">Class</span> javascript
	<span style="color: #0600FF;">Inherits</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">Page</span>
&nbsp;
	<span style="color: #FF8000;">Private</span> <span style="color: #008000;">files</span> <span style="color: #FF8000;">As</span> Stack
&nbsp;
	Protected <span style="color: #0600FF;">Sub</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">Load</span>
		<span style="color: #008000;">files</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> Stack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;application/x-javascript&quot;</span>
		Response.<span style="color: #0600FF;">Write</span><span style="color: #000000;">&#40;</span>GetFile<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;js/Contacts.js&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
	<span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Function</span> GetFile<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> path <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
		<span style="color: #0600FF;">Dim</span> result <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
		<span style="color: #0600FF;">Dim</span> filespec <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">PhysicalApplicationPath</span> <span style="color: #008000;">&amp;</span> path.<span style="color: #0600FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;/&quot;</span>, <span style="color: #808080;">&quot;\&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
		filespec <span style="color: #008000;">=</span> IO.<span style="color: #0000FF;">Path</span>.<span style="color: #0000FF;">GetFullPath</span><span style="color: #000000;">&#40;</span>filespec<span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">'if the file is already loaded, then exit</span>
		<span style="color: #0600FF;">If</span> <span style="color: #008000;">files</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>filespec<span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span> <span style="color: #FF8000;">Return</span> <span style="color: #808080;">&quot;&quot;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">'add this file to the loaded list</span>
		<span style="color: #008000;">files</span>.<span style="color: #0000FF;">Push</span><span style="color: #000000;">&#40;</span>filespec<span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">'load the file</span>
		<span style="color: #0600FF;">Dim</span> s <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
		<span style="color: #0600FF;">Dim</span> b <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span>
		<span style="color: #0600FF;">Try</span>
			<span style="color: #0600FF;">Dim</span> sr <span style="color: #FF8000;">As</span> StreamReader <span style="color: #008000;">=</span> <span style="color: #008000;">File</span>.<span style="color: #0000FF;">OpenText</span><span style="color: #000000;">&#40;</span>filespec<span style="color: #000000;">&#41;</span>
			<span style="color: #0600FF;">Do</span>
				b <span style="color: #008000;">=</span> <span style="color: #0600FF;">True</span>
				s <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> vbCrLf
				<span style="color: #0600FF;">If</span> s.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;//LIBRARY:&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span>
					b <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span>
				<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
				<span style="color: #0600FF;">If</span> s.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;//REQUIRES:&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span>
					b <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span>
					s <span style="color: #008000;">=</span> s.<span style="color: #0600FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;//REQUIRES:&quot;</span>, <span style="color: #808080;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
					result <span style="color: #008000;">=</span> result <span style="color: #008000;">&amp;</span> GetFile<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;..&quot;</span> <span style="color: #008000;">&amp;</span> s<span style="color: #000000;">&#41;</span>
					s <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
				<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
				result <span style="color: #008000;">=</span> result <span style="color: #008000;">&amp;</span> s
			<span style="color: #0600FF;">Loop</span> <span style="color: #0600FF;">While</span> sr.<span style="color: #0000FF;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> &lt;&gt; <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span> <span style="color: #804040;">And</span> <span style="color: #804040;">Not</span> b
			result <span style="color: #008000;">=</span> result <span style="color: #008000;">&amp;</span> sr.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> vbCrLf
			sr.<span style="color: #0600FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0600FF;">Catch</span> ex <span style="color: #FF8000;">As</span> Exception
			result <span style="color: #008000;">=</span> vbCrLf <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;/* Error loading file: &quot;</span> <span style="color: #008000;">&amp;</span> filespec <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot; */&quot;</span> <span style="color: #008000;">&amp;</span> vbCrLf
		<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Try</span>
&nbsp;
		<span style="color: #FF8000;">Return</span> result
	<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
&nbsp;
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span></pre></td></tr></table></div>

<p>This could be easily updated to work for CSS files as well.</p>
<div class="link-summarizer"><p><strong>Links in this post</strong><ul><li><a href='/2009/10/recursive-script-inclusion-with-require-c/'>/2009/10/recursive-sc...usion-with-require-c/</a></li></ul></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.bbqiguana.com/2009/10/recursive-script-inclusion-with-require/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

