views:

247

answers:

3

I have been able to get javascript intellisense working correctly for a 'class' prototype defined like this:

function GetCustomerList()
{
}

GetCustomerList.prototype = 
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: "",
        }
    ,
    NUM_RECORDS: 0,
    START_RECORD: 0,
    END_RECORD: 0
};

I can type something like:

var req = new GetCustomerList();
req.HEADER.RETURN_CODE = 100;

And Visual Studio's intellisense knows about the HEADER property, and its own properties named 'RETURN_CODE' and 'RETURN_MESSAGE'. I can do:

req.NUM_RECORDS = 50;

With intellisense working perfectly.

So intellisense works with complex nested types - great. However is it possible to get intellisense with an array of complex types?

Example:

function Customer()

Customer.prototype = {

    NAME: "",
    ADDRESS: "",
    ID: 0
};

function GetCustomerList()
{
}

GetCustomerList.prototype = 
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: "",
        }
    ,
    NUM_RECORDS: 0,
    START_RECORD: 0,
    END_RECORD: 0,
    CUSTOMERS: [ new CUSTOMER() ]
};

Where I have an array of the type 'CUSTOMER' which I have also defined a prototype for. I'd like to be able to type things like:

req.CUSTOMER[ 0 ].NAME

And have intellisense prompt me that 'NAME' is a property available for this array.

Is this possible?

+2  A: 

UPDATE:

As you have already noticed, IntelliSense works for your complex types just fine, but does not work at the Array. Even if you create an array of intrinsic types like String, it still does not work.

I have researched this topic thoroughly, in theory, this should be possible, but it's not.
As a test, create javascript file name it "customers.js" and include the following:

function Customer() {
    /// <summary>This is my custom intellisense for the Customer type</summary>
    ///<field name="NAME" type="String">The Customer's name</field>
    ///<field name="ADDRESS" type="String">The customer's address</field>
    ///<field name="ID" type="String">The ID number</field>
}
Customer.prototype = {
    NAME: "",
    ADDRESS: "",
    ID: 0
};

function CustomerList() {
    /// <summary>The List of Customers</summary>
    ///<field name="HEADER" type="String">The header</field>
    ///<field name="CUSTOMERS" type="Array" elementType="Customer" >The list of customers in an Array</field>
}

CustomerList.prototype =
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: ""
    },
    NUM_RECORDS: 0,
    START_RECORD: 0,
    END_RECORD: 0,
    CUSTOMERS: [new Customer()]
};

Then reference this file inside <script src="customers.js"/>

or /// <reference path="customer.js" /> inside another JS file.

See how the intellisense show the summaries correctly, but when it comes to the array, nothing.

var custList = new CustomerList();

custList.CUSTOMERS // intellisense
custList.CUSTOMERS[0] // no intellisense as you already know
Jose Basilio
actually it's nothing like that post. That post deals with getting intellisense to work at all with the config at VS. This deals with the actual syntax needed to take advantage of intellisense.
Adam
I updated my original post after researching this further.
Jose Basilio
A: 

There is no way to get this to work in VS2008. It will work in the next version of VS.

Alan Oursland
A: 

Though VS doesn't support it, you may want to consider the following work-around.

I changed the example script to:

function Customer(obj) {
    /// <summary>This is my custom intellisense for the Customer type</summary> 
    ///<field name="NAME" type="String">The Customer's name</field> 
    ///<field name="ADDRESS" type="String">The customer's address</field>
    ///<field name="ID" type="String">The ID number</field>
    if (obj) return obj;
}

Customer.prototype = {
    NAME: '',
    ADDRESS: '',
    ID: 0
};

function CustomerList() {
    /// <summary>The List of Customers</summary> 
    ///<field name="HEADER" type="String">The header</field> 
    ///<field name="CUSTOMERS" type="Array" elementType="Customer" >The list of customers in an Array</field> 
}

CustomerList.prototype =
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: ''
    },
    CUSTOMERS: []
}; 

(Note the change in constructor - not sure about the XML comment for intellisense).

Then you can reference it like this (with intellisense):

var list = new CustomerList();

var cust = new Customer();
cust.NAME = 'john';
list.CUSTOMERS.push(cust);

var cust1 = new Customer(list.CUSTOMERS[0]);
alert(cust1.NAME);

var cust2 = new Customer({ NAME: 'Mark' });
alert(cust2.NAME);

The argument against this is that you need the parameter in the constructor.

Dawid