Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - intellisense for javascript custom object

I created the following javascript object:

var Content = Content || {};

// Constructor defines properties and inits object
Content.ProductManager = function () {
    // ...
};


Content.ProductManager.prototype = function () {

    // 
    // private members
    // 


    var setProductAsPreviewed = function (args) {
        // code omitted for brevity
        // ....
    };


    //
    // public members
    // 

    return {
        setProductAsPreviewed: setProductAsPreviewed
    };

} (); 

The object passed to setProductAsPreviewed has the following properties:

args = {
    productId: int,
    productName: string,
    updateDate: date,
    saveItems: bool
};

I want to include XML comments so I can get intellisense for the argument passed to function setProductAsPreviewed:

var productManager = new window.Content.ProductManager();
// show intellisense when typing the following:
productManager.setProductAsPreviewed( 

This thread shows how to do it for simple args (string, int, ...), but how to do it for a complex object? I'm using Visual Studio 2010.

like image 533
Rui Jarimba Avatar asked May 23 '13 08:05

Rui Jarimba


2 Answers

As far as I know, you can't tell IntelliSense what fields and methods are on a generic variable if it is being used as a parameter.

If the variable was an array, you could define it like this:

function funcWithArrayArg(arrayArg) {
    /// <param name="arrayArg" type="Array" elementType="Number">An array of numbers</param>
}

In VS2012 you can annotate objects as well, like so (you can annotate fields on functions used as object constructors, as I demonstrate below but the docs say nothing about anonymous objects like this):

var args = {
    /// <field type="Number">Product ID</field>
    productID: int
};

Neither of these approaches really do what you want, as the second approach wouldn't give you intellisense on the function argument and you're using VS2010 anyway.

I think your best bet is to define a custom object to be used as an argument object just for that function, after all this is how you would do it in other languages if you wanted to create a custom object as a parameter with intellisense. It might look something like this:

function ProductPreviewArgs(productId, productName, updateDate, saveItems) {
    /// <summary>Creates an object for use as the sole argument to the setProductAsPreviewed function</summary>
    /// <param name="productId" type="Number" optional="false">The Product ID</param>
    /// <param name="productName" type="String" optional="false">The Product Name</param>
    /// <param name="updateDate" type="Date" optional="false">The date the product was last updated</param>
    /// <param name="saveItems" type="Boolean" optional="false">Specifies whether or not to save the items</param>
    /// <returns type="ProductPreviewArgs">An object intended for use as the sole argument to the setProductAsPreviewed function</returns>
    /// <field name="productId" type="Number">The Product ID</field>
    /// <field name="productName" type="String">The Product Name</field>
    /// <field name="updateDate" type="Date">The date the product was last updated</field>
    /// <field name="saveItems" type="Boolean">Specifies whether or not to save the items</field>
    this.productId = productId;
    this.productName = productName;
    this.updateDate = updateDate;
    this.saveItems = saveItems;
}

You would get intellisense on the object (which will show what you have put in the returns element) here:

setProductAsPreviewed(

If you then decide to create a new object, you would get IntelliSense here (which will show you the descriptions for each parameter one by one as you add them):

setProductAsPreviewed(new ProductPreviewArgs(

I'm not entirely sure whether the type attribute on the returns element will actually work like that, it does in VS2012 and as you might have come to expect by now, the docs are annoyingly bare on this subject; and I don't have a copy of VS2010 to test any of this on right now.

like image 127
Sean Airey Avatar answered Sep 20 '22 14:09

Sean Airey


You can create a separate IntelliSense file, that looks something like this

intellisense.annotate(Content, {
  'setProductAsPreviewed ': function() {
    /// <signature>
    ///   <summary>Summary<summary>
    ///   <param name="args" type="ComplexObject">some text here
    /// </signature>
   }
})

I believe this should work with some modification

like image 39
Johan Avatar answered Sep 20 '22 14:09

Johan