asp.net mvc 开发人员的 javascript 最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7169227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript best practices for asp.net mvc developers
提问by Ben Foster
Apologies for the question being so subjective.
很抱歉这个问题太主观了。
I'm looking for some practical examples of how to manage javascript better in asp.net mvc applications.
我正在寻找一些关于如何在 asp.net mvc 应用程序中更好地管理 javascript 的实际示例。
Prior to jQuery I tried to avoid using javascript as much as possible. Now I'm using it a lot but feel I may have missed some best practices / recommendations for structuring my javascript code in larger web applications.
在使用 jQuery 之前,我尽量避免使用 javascript。现在我经常使用它,但我觉得我可能错过了一些在大型 Web 应用程序中构建我的 javascript 代码的最佳实践/建议。
Some things that I am guilty of and want to move away from:
我有罪并想摆脱的一些事情:
- Inline javascript in my views. I'm not talking masses of code but it's not uncommon for me to have some initialization code on each view.
- One large "site" javascript file. As the list of plugins used in my application grows (for example jquery ui), so too does my "startup" function which is initializing each plugin. Often this is to avoid issue 1 (adding javascript inline). Perhaps a javascript file per view is a better solution?
- Everything seems very procedural. I'm used to working in an object oriented way in .net, but when it comes to javascript this seems to go out the window. Now I know OOP is certainly possible, and encouraged with javascript but I could do with seeing some practical examples of how I should be doing it in the context of a real web application.
- Namespacing / project structure - I've seen a few examples of javascript namespacing (by virtue of objects) and like the approach but again could do with some more background knowledge.
- 在我的观点中内联 javascript。我不是在谈论大量的代码,但在每个视图上都有一些初始化代码对我来说并不少见。
- 一个大的“站点”javascript 文件。随着我的应用程序中使用的插件列表的增长(例如 jquery ui),我正在初始化每个插件的“启动”功能也在增长。通常这是为了避免问题 1(添加内联 javascript)。也许每个视图的 javascript 文件是更好的解决方案?
- 一切似乎都非常程序化。我习惯于在 .net 中以面向对象的方式工作,但是当涉及到 javascript 时,这似乎消失了。现在我知道 OOP 肯定是可能的,并鼓励使用 javascript,但我可以通过查看一些实际示例来说明我应该如何在真正的 Web 应用程序的上下文中执行它。
- 命名空间/项目结构 - 我已经看到了一些 javascript 命名空间的例子(通过对象)并且喜欢这种方法,但同样可以用一些更多的背景知识来做。
Any sites, books etc. that people can recommend would be appreciated.
人们可以推荐的任何网站、书籍等将不胜感激。
回答by Tri Q Tran
I thought I'd throw in my 2 cents.
我以为我会投入我的 2 美分。
Best practicesuggest not to use JScript in views, and this I found could be averted by using HTML helpers that contains JScript or push JScript into JS files and initialize it in your HTML helper. This is what third party MVC control dev (eg. Telerik) uses. If you do decide to push the JScript into your controller, you will find it gets messy pretty quickly.
I often find myself in the same scenario where I have a JS file for each view, but this can be consolidated to have one JS per module. This again is up to how complex your code can be. If you find that many views have the same initialization, then you can go down the base view approach. This is also considered best practice.
You will find that JScript is quite OOand this is to do with how JScript uses JSON. I find that the flexibility of JScript causes it to appear to lean away from OOP. My limited experience with JScript is to keep it short and concise and this will re-enforce re-usable JScripts.
I think this can be over-come with practice in JScript. The biggest issue I find here is not with name-spacing, but how to avoid duplicating/double loading of JScripts. If views are to be "self-contained", the should in theory load their own JScripts, but this will cause JScripts to be loaded multiple times or even create conflicts. Again, a good HTML helper can save you here (aplogies, I don't have a good example, but Google it and you can't go wrong).
最佳实践建议不要在视图中使用 JScript,我发现可以通过使用包含 JScript 的 HTML 帮助程序或将 JScript 推送到 JS 文件并在您的 HTML 帮助程序中对其进行初始化来避免这种情况。这是第三方 MVC 控制开发者(例如 Telerik)使用的。如果您决定将 JScript 推送到您的控制器中,您会发现它很快就会变得混乱。
我经常发现自己处于相同的场景中,每个视图都有一个 JS 文件,但这可以合并为每个模块一个 JS。这再次取决于您的代码的复杂程度。如果你发现很多视图都有相同的初始化,那么你可以走下基本视图的方法。这也被认为是最佳实践。
您会发现JScript 非常面向对象,这与 JScript 使用 JSON 的方式有关。我发现 JScript 的灵活性使它看起来远离 OOP。我对 JScript 的有限经验是保持简短和简洁,这将加强可重用的 JScript。
我认为这可以通过 JScript 中的练习来克服。我在这里发现的最大问题不是名称间距,而是如何避免重复/双重加载 JScript。如果视图要“自包含”,理论上应该加载自己的 JScript,但这会导致 JScript 被多次加载甚至产生冲突。再一次,一个好的 HTML 帮助程序可以在这里拯救你(抱歉,我没有一个很好的例子,但谷歌它,你不会出错)。
Overall, JScript is very powerful and opens up web programming to a whole new level. JScript will be more and more a part of the web platform, especially when everyone moves into HTML 5. With MVC and JSript, there is 100 different ways to do exactly the same thing and best practice can only take you so far. The rest is up to you to decide depending on what you're trying to achieve, who your target audience are and what browsers you're supporting.
总的来说,JScript 非常强大,将 Web 编程打开到了一个全新的水平。JScript 将越来越多地成为 Web 平台的一部分,尤其是当每个人都转向 HTML 5 时。使用 MVC 和 JSript,有 100 种不同的方法可以做完全相同的事情,而最佳实践只能带您到此为止。剩下的由您决定,具体取决于您要实现的目标、目标受众是谁以及您支持的浏览器。
回答by Jaime
I've used the approach described here:
我使用了这里描述的方法:
I'd be curious to know what you think of it.
我很想知道您对此有何看法。
Ref to a similar question on SO
参考关于 SO 的类似问题
Is there a better way to organise javascript in a single file with specific code for multiple pages?
回答by Hugo Zapata
For me, libraries like KnockoutJShave helped a lot to organize my JS code, it's really powerful, i have used it for multiple projects and it has worked really well. (I'm also an asp.net mvc developer but this applies to other technologies too, besides, the guy who invented it has a lot of experience with aspnet mvc)
对我来说,像KnockoutJS这样的库对组织我的 JS 代码有很大帮助,它真的很强大,我已经将它用于多个项目,而且效果非常好。(我也是一个 asp.net mvc 开发人员,但这也适用于其他技术,此外,发明它的人在 aspnet mvc 方面有很多经验)