简体   繁体   English

Visual Studio 2008/2010中对jQuery闭包的Intellisense支持{

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

I'm trying to get Intellisense to correctly work for closure. 我正在尝试让Intellisense正确地用于关闭。 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. 但问题是Intellisense没有发现在执行函数时传递了jQuery。 Adding $ = jQuery in the above code fixes the problem, but that's just poor execution, IMHO. 在上面的代码中添加$ = jQuery可以解决问题,但这只是糟糕的执行,恕我直言。

Anyone here got this working without resorting to embedded ASP server tags (this is a standalone JS file)? 这里的任何人都可以在不使用嵌入式ASP服务器标签的情况下工作(这是一个独立的JS文件)? Something preferably not including modifying existing code other than some odd /// <option .../> -like solution? 有些东西最好不包括修改现有代码而不是某些奇怪的/// <option .../> -like解决方案?

It isn't clear in your post or your comments, but at the top of your .js file, did you add: 您的帖子或评论中不清楚,但在.js文件的顶部,您是否添加了:
/// <reference path="jquery.vsdoc.js" />
to the top of your file? 到你的文件的顶部?

ScottGu's blog has more on intellisense in external libraries (not jQuery-specific). ScottGu的博客更多关于外部库中的intellisense(不是jQuery特定的)。

Also, here's another possible solution, is this what you mentioned with $=jQuery ?: 另外,这是另一个可能的解决方案,这是你用$=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 在此处找到: 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. 如果您正在为您的jQuery插件开发IDE查看Visual Studio 2010,那么您已做出了正确的选择。 Here are the details for the setup: 以下是设置的详细信息:

  1. Download the jquery and the respective jquery.vsdoc in the same directory of your project. 在项目的同一目录中下载jquery和相应的jquery.vsdoc。 You can download the latest version of the jQuery files from http://www.asp.net/ajaxlibrary/cdn.ashx . 您可以从http://www.asp.net/ajaxlibrary/cdn.ashx下载最新版本的jQuery文件。 Here are the links for the latest jQuery links from above CDN: 以下是CDN上面最新jQuery链接的链接:

    • 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) . 在我的开发环境中,我使用未压缩的jquery文件重命名为jquery.js (删除文件名中的版本信息[-1.7.1],并记住也从vsdoc文件名中删除版本信息)

  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. 记得使用/// <param name="$" type="jQuery" />作为插件关闭的第一行,正如我在上面的代码中所演示的那样。 It all works for me in Visual studio 2010 SP1. 这一切都适用于Visual Studio 2010 SP1。

Visit My jQuery Plugin Site and Blog 访问我的jQuery插件网站博客

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

I'm surprised this doesn't work in VS2010 (I don't think you will be able to make it work in VS2008). 我很惊讶这在VS2010中不起作用(我认为你不能在VS2008中使它工作)。

You could try adding an xml doc comment to the beginning closure to define the param type. 您可以尝试将xml doc注释添加到开始闭包以定义param类型。 Something like this: 像这样的东西:

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

(I don't know what the class name for the jquery object is -- or if there is even one available). (我不知道jquery对象的类名是什么 - 或者是否有一个可用的类名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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