简体   繁体   中英

Intellisense support in Visual Studio 2008/2010 for jQuery closures {

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?

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

if you are looking at Visual Studio 2010 for your jQuery plugin development IDE, you have made the right choice. Here are the details for the setup:

  1. Download the jquery and the respective jquery.vsdoc in the same directory of your project. You can download the latest version of the jQuery files from http://www.asp.net/ajaxlibrary/cdn.ashx . Here are the links for the latest jQuery links from above CDN:

    • http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.js
    • http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js
    • http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1-vsdoc.js

    In my development environment I use the uncompressed jquery file renamed to jquery.js (removing the version information [-1.7.1] in the file name, and remember to remove the version information from the vsdoc file name too) .

  2. Create your plugin file with its first line containing the line

     /// <reference path="/path/to/jquery.js"> 
  3. Create the plugin code with closure. Here is the full skeleton of a plugin:

     /// <reference path="jquery.js" /> (function ($) { /// <param name="$" type="jQuery" /> jQuery.fn.gallery = function () { return this.each(function () { // your code here }); }; })(jQuery); 
  4. Remember to use /// <param name="$" type="jQuery" /> as the first line in the closure of the plugin as I have demonstrated in the code above. It all works for me in Visual studio 2010 SP1.

Visit My jQuery Plugin Site and Blog

但在安装此修补程序之前,请确保在系统中安装了SP1。

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM