将 jquery/javascript 与 asp.net MVC 视图分开?

发布于 2024-08-09 00:10:53 字数 345 浏览 2 评论 0原文

我试图找到将我的脚本(即 jquery/javascript)与 asp.net MVC 中的视图(html)分开的最佳方法...

有人知道这样做的最佳方法吗?

我在我的视图中看到很多代码味道...

我过去使用过 JavascriptMVC,它做得非常好,也许将它们结合起来是个好主意,但考虑到我可能不会使用 javascriptmvc 的视图部分这不是正确的方向。

我真的很喜欢一些想法,并帮助找到使脚本远离视图等的最佳方法。

我正在使用许多外部模块,如 YUI 和 jquery,所以如果我需要采用另一个概念/layer - 我没有看到问题

期待任何帮助

谢谢

I am trying to found it the best way to keep my scripting i.e. jquery/javascript separate from my view (html) in asp.net MVC...

Does anyone have any idea the best way of doing this??

I am seeing alot of code smell in my views ...

I have used in the past JavascriptMVC which does a really good job and maybe its a good idea to combine them but considering i won't be using the view part of the javascriptmvc maybe its not the right direction to take..

I would really love some ideas and help on the best way of keeping scripting away from views etc..

I am using a number of external modules like YUI and jquery so if i need to employ another concept/layer - i don't see a problem

Look forward to any help

Thanksa

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

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

发布评论

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

评论(2

情定在深秋 2024-08-16 00:10:53

我们可能(也可能没有)在如何将 javascript 与标记分开方面存在哲学差异。在我看来,只要 JavaScript 不嵌入到标记本身中,页面上就可以包含 JavaScript。如果可能的话,它应该在它自己的文件中关闭,但是当它直接引用特定于视图的元素时,我更喜欢将它放在视图文件本身中。

这是我使用的策略。

  1. 将通用代码重构为插件。将插件放在单独的 JS 文件中,并在需要的地方包含它们。
  2. 使用单独的 ContentPlaceHolder (CPH) 来包含母版页中结束正文标记内的脚本和代码。这使 JS 在页面上功能上保持独立,并确保它在页面底部加载。
  3. 将常见的 JS 包含在母版页中脚本 CPH 的正上方。
  4. 放置每个页面通用的代码,但在母版页中的通用包含之后包含特定于页面的引用。
  5. 尽可能避免使用实时处理程序等将 JS 放入部分视图中。
  6. 将所有代码(包含和函数定义除外)包装在 $(function() { } 内(文档准备就绪)。
  7. 尽可能使用通用代码的内容分发网络 (CDN) 版本。

We may (or may not) have a philosophical difference in how separate to keep javascript from mark up. In my opinion, it's ok to have javascript on the page as long as its not embedded in the markup itself. If possible, it should be off in it's own files but when it directly references view-specific elements, I prefer to have it in the view file itself.

Here's the strategy that I use.

  1. Refactor common code to plugins. Put plugins in a separate JS file and include them where needed.
  2. Use a separate ContentPlaceHolder (CPH) for script includes and code just inside the closing body tag in the master page. This keeps the JS functionally separate on the page and ensures that it is loaded at the bottom of the page.
  3. Put common JS includes just above the script CPH in the master page.
  4. Put code common to every page, but containing page-specific references after the common includes in the master page.
  5. Avoid putting JS in partial views by using live handlers, etc. where possible.
  6. Wrap all code, except includes and function definitions, inside $(function() { } (document ready).
  7. Use content-distribution network (CDN) versions of common code where possible.
站稳脚跟 2024-08-16 00:10:53

如果您将现有代码的代码片段粘贴到问题中,会更容易为您提供帮助。看看 这篇博文了解如何将 javascript 与 html 分开。

It will be easier to help you if you paste a code snippet of your existing code in the question. Look at this blog post to see how you can separate your javascript from your html.

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