简体   繁体   English

Asp.net MVC 3.0中的JQuery模式和最佳实践

[英]JQuery patterns and best practices in Asp.net MVC 3.0

I'm working with an Asp.net MVC 3.0 application and use JQuery + JSON . 我正在使用Asp.net MVC 3.0应用程序并使用JQuery + JSON。 I'm planing to use jquery post and get methods to send/back data to server.As a result, my JQuery Code base will be large and should be maintainable. 我正在计划使用jquery post并获取将数据发送/返回到服务器的方法。因此,我的JQuery代码库将很大并且应该是可维护的。

so that what are the best practices and design patterns when using JQuery with ASP.net MVC 3.0 ? 那么在使用JQuery和ASP.net MVC 3.0时,最佳实践和设计模式是什么?

Here are a few tips: 以下是一些提示:

  • Always try to place javascript in separate js files and avoid placing it in views. 总是尝试将javascript放在单独的js文件中,并避免将其放在视图中。
  • Never hardcode urls in your javascript files. 切勿在javascript文件中对网址进行硬编码。 Always use Url helpers when generating urls. 生成网址时始终使用Url帮助程序。
  • For javascript code that could be reused in multiple places, prefer to write a jQuery plugin . 对于可以在多个地方重用的javascript代码,更喜欢编写jQuery插件
  • Before writing some code, search whether someone hasn't yet written a jQuery plugin for it. 在编写一些代码之前,先搜索某人是否尚未为其编写jQuery插件。
  • Minify your javascript files when running in release mode 在发布模式下运行时缩小javascript文件
  • Try to reduce the number of AJAX requests: for example prefer one AJAX request which sends/receives bigger data compared to multiple AJAX requests with smaller data. 尝试减少AJAX请求的数量:例如,与具有较小数据的多个AJAX请求相比,更喜欢发送/接收更大数据的一个AJAX请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM