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 metho开发者_Go百科d? Or is this a common practice?
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.
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.
精彩评论