从 JavaScriptSerializer 生成时的 Visual Studio Intellisense for JavaScript
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Javascript 智能感知是通过解析脚本本身生成的,因此除非您在脚本(或引用的脚本)中内联定义了属性,否则您将看不到智能感知。
如果您想将某些结构与智能感知一起使用,但最终要通过动态构造来提供,那么您可以将它们存根到不同的 .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:
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.
等待 VSNext 或某个能够实现这种情况的补丁。目前不支持将 javascript 与服务器端代码混合的场景中的 Intellisense。
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.