Metro Smurf
Metro Smurf

Reputation: 38335

Visual Studio Intellisense for JavaScript when generated from JavaScriptSerializer

What is the correct way for getting JavaScript Intellisense in Visual Studio 2010 when creating a client side object with the JavaScriptSerializer?

For example, I have a class named Record with several properties; I'm generating a collection of Records and then serializing them using the JavaScriptSerializer.

Code Behind

public string JsonRecords
{
    get
    {
        var js = new System.Web.Script.Serialization.JavaScriptSerializer();
        return js.Serialize( Records );
    }
}

ASPX page

<script>
  // mocks the Record object
  var records = [{ "Date": "", "Latitude": 0, "Longitude": 0 }];

  // sets the Record object
  records = <%= JsonRecords %>;
</script>

When I pre-fill the JS records variable to mock the Records class, I get complete intellisense support with Visual Studio.

This works, but it feels dirty. Is there a more appropriate method? Or is this a common practice?

Upvotes: 2

Views: 564

Answers (2)

ahin4114
ahin4114

Reputation: 483

Javascript intellisense is generated by parsing the script itself, therefore unless you have defined the properties inline in the script (or a referenced script) then you won't see intellisense.

If there were structures which you wanted to use with intellisense but were ultimately going to provide through a dynamic construct, then you could stub them out in a different .js file and then include a reference tag in your file thus:

/// <reference path="../xxx.js" />

This would be treated as a comment in the client, but Visual Studio would pick it up when you're working in the code. Minifiers/uglifiers will remove these comments before they hit production, so they won't impact performance.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038760

What's the correct way for getting JavaScript Intellisense in Visual Studio 2010 when creating the object by the JavaScriptSerializer?

Wait for VSNext or some patch that will enable such scenario. Currently Intellisense in scenarios when mixing javascript with server side code is not supported.

Upvotes: 2

Related Questions