简体   繁体   中英

Mimicking Casting with Visual Studio's JavaScript IntelliSense

I am passing in a jQuery object into a function from another file via an array like the following:

$(document).bind("loadStoreDisplayCallGoals", function(source, urlParams)
{
    var selectedStoreDocument = urlParams["storeDocument"];
}

selectedStoreDocument should be a jQuery object, however Visual Studio Intellisense will never recognize it as such. I tried adding extending selectedStoreDocument with $.extend :

// cast selectedStoreDocument to a jQuery type
$.extend(selectedStoreDocument, $);

However, extending selectedStoreDocument wiped out all of my jQuery methods ( .each , .find , etc.).

How can I get selectedStoreDocument to appear as a jQuery object in IntelliSense? Note that I am working in Visual Studio 2010.

I created a separate file for utility functions, and a second file for the utility functions + VSDoc.

utilities.js:

function castToJQuery(item)
{
    return item;
}

utilities-vsdoc.js:

function castToJQuery(item)
{
    /// <summary>
    ///     1: $(item) - "Casts" the specified item to a jQuery object for the sake of Intellisense
    /// </summary>
    /// <returns type="jQuery" />
    return $("dummy");
}

Now I can call castToJQuery in any of my downstream files to make Visual Studio think a dynamic property is a jQuery object.

var selectedStoreDocument = castToJQuery(urlParams["storeDocument"]);
selectedStoreDocument.find("products");

Visual Studio now works with Intellisense for my dynamic urlParams["storeDocument"].

You cannot get intellisense for dynamically added properties. You need to define them statically (in a vsdoc or js file):

$.selectedStoreDocument = function() {
     ///<summary>A Selected Store Document</summary>
};

You can specify documentation information for a variable like this:

$(document).bind("loadStoreDisplayCallGoals", function(source, urlParams)
{
    /// <var type="jQuery"/>
    var selectedStoreDocument = urlParams["storeDocument"];
    selectedStoreDocument._
}

For more information see http://msdn.microsoft.com/EN-US/library/hh542722(VS.110).aspx

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