views:

651

answers:

6

I'm trying to get Intellisense to correctly work for closure. As a plugin author, I always use a closure to create an isolated environment for my plugin code:

(function($) {
  // code here
})(jQuery);

But the problem here is that Intellisense doesn't pick up that jQuery is being passed in the execution of the function. Adding $ = jQuery in the above code fixes the problem, but that's just poor execution, IMHO.

Anyone here got this working without resorting to embedded ASP server tags (this is a standalone JS file)? Something preferably not including modifying existing code other than some odd /// <option .../>-like solution?

A: 

I dont know if you have read Roni's or Rick's articles on this - I assume you have already made intellisense work in VS?

Article - http://netrsc.blogspot.com/2009/01/jquery-vs-2008-and-intellisense.html Article - http://www.west-wind.com/WebLog/posts/251271.aspx

Not being a jQuery person - having just this was nice

codemypantsoff
Yes, I've already installed the hotfix on 2008 and it works, also using 2010, which has it already plugged in.The problem is when I try to pass objects as parameters into a function inside closures like the one described above. Even when I'm executing and passing in the jQuery object, it still won't recognize `$ === jQuery` unless I add a `$ = jQuery` at the top of the function body.
Krof Drakula
Could have something to do with other js libraries using it too - the parser might not be able to understand it till it has some indication what library it is - I wouldn't be too shure there is a fix for this.....
codemypantsoff
There's no other library involved here, since VS treats every JS file as standalone unless you add explicit references via `/// <reference path="..."/>`. But I've found some additional info on Rich Strahl's blog (link in answer) that might be a workaround.
Krof Drakula
+4  A: 

It isn't clear in your post or your comments, but at the top of your .js file, did you add:
/// <reference path="jquery.vsdoc.js" />
to the top of your file?

ScottGu's blog has more on intellisense in external libraries (not jQuery-specific).

Also, here's another possible solution, is this what you mentioned with $=jQuery?:

(function($) {  // private closure;  <% /*debug*/ if (false) { %> 
    $ = jQuery;
    // <% } /*end debug*/ %>
    $(function() {
        // do stuff
    });
})(jQuery);

Found here: http://blog.jeroenvanwarmerdam.nl/post/IntelliSense-VS08-within-private-closure.aspx

Jim Schubert
Yes, I have added the reference, but it doesn't apply to solving this particular problem (jQuery is referenced fine, but the reference withing the closure doesn't work). That's exactly the type of conditional server-side statements I'm trying to not use, since I'm using static Javascript files, not code withing ASPX/ASCX pages.
Krof Drakula
It seems this specific case is a limitation of the Intellisense engine itself. It won't recognize that the function is being executed with jQuery passed in as a parameter. This problem, however, cannot be solved for static Javascript files, but only for processed files, such as ASPX and ASCX.
Krof Drakula
A: 

But before installing the hotfix make sure you have SP1 installed in your system.

Ravia
SP1 *was* installed before. Like I said, Intellisense works, but not for this specific case.
Krof Drakula
A: 

Take a look at this: http://weblogs.asp.net/bradvincent/archive/2008/04/28/better-jquery-intellisense-in-vs2008.aspx

Hameds
I've read that article before too, but sadly it still doesn't cover the case listed above.
Krof Drakula
A: 

I'm surprised this doesn't work in VS2010 (I don't think you will be able to make it work in VS2008).

You could try adding an xml doc comment to the beginning closure to define the param type. Something like this:

/// <param name="$" type="Jquery" />

(I don't know what the class name for the jquery object is -- or if there is even one available).

Alan Oursland
I'll give that a go, thanks.
Krof Drakula
A: 

I'm experiencing the same problem - has anyone found a solution to this?

Ken
The only way to resolve this using the current Intellisense is to Jim's answer above, but you have to be inside an `ASP*` page for server script tags to work. IMHO, not a very pretty nor elegant solution, more of a workaround than anything else. Would be nice if you could add a triple-slash comment with special variable type hints in JS files. I haven't tried Alan's solution using `///<param/>`, that might work.
Krof Drakula