Infolink

 

Search This Blog

Aug 26, 2012

Improve reference efficiency in JavaScript files inside a web project

In Visual Studio 2010 you can reference other JavaScript files inside current JavaScript file like the following, to get the IntelliSense.

/// <reference path="YourScript.js" />

What if you have many JavaScript files and you have to reference many project wide JavaScript files inside each file?  You can put the following block in each of the JavaScript files that need the reference like following:

/// <reference path="YourScript1.js" />
/// <reference path="YourScript2.js" />
/// <reference path="YourScript3.js" />
/// <reference path="YourScript4.js" />


But this would not be efficient especially some referencing file name is going to change, such as when JQuery library version changes.  The trick is to create a project wide reference JavaScript file, and explicitly reference this file from other JavaScript files.  For example, we can create a file called reference.js which contains the above code.  Then for other JavaScript files, we can reference it by dragging and drop the reference.js file into the JavaScript editor, which will generate the following:

/// <reference path="Scripts/AppJsReference.js" />

Basically, Visual Studio 2010 recursively check the references included in the referenced JavaScript files to find the current JavaScript file's references.  I hope this can ease some pain in the development.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...