从 JavaScriptSerializer 生成时的 Visual Studio Intellisense for JavaScript

发布于 2024-12-01 13:04:50 字数 835 浏览 1 评论 0原文

使用 JavaScriptSerializer 创建客户端对象时,在 Visual Studio 2010 中获取 JavaScript Intellisense 的正确方法是什么?

例如,我有一个名为 Record 的类,它具有多个属性;我正在生成 Records 集合,然后使用 JavaScriptSerializer 对它们进行序列化。

代码隐藏

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

ASPX 页面

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

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

当我预填充 JS 记录变量以模拟 Records 类时,我获得了 Visual Studio 的完整智能感知支持。

这可行,但感觉很脏。有没有更合适的方法呢?或者这是一种常见做法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-12-08 13:04:51

Javascript 智能感知是通过解析脚本本身生成的,因此除非您在脚本(或引用的脚本)中内联定义了属性,否则您将看不到智能感知。

如果您想将某些结构与智能感知一起使用,但最终要通过动态构造来提供,那么您可以将它们存根到不同的 .js 文件中,然后在您的文件中包含引用标记:

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

这将被视为客户端中的注释,但当您在代码中工作时,Visual Studio 会拾取它。缩小器/丑化器将在这些注释投入生产之前删除它们,因此它们不会影响性能。

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.

锦上情书 2024-12-08 13:04:50

在 Visual 中获取 JavaScript Intellisense 的正确方法是什么
Studio 2010何时通过JavaScriptSerializer创建对象?

等待 VSNext 或某个能够实现这种情况的补丁。目前不支持将 javascript 与服务器端代码混合的场景中的 Intellisense。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文