简体   繁体   English

asp.net mvc开发人员的javascript最佳实践

[英]javascript best practices for asp.net mvc developers

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. 在jQuery之前,我尽量避免使用javascript。 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. 现在我使用它很多,但我觉得我可能错过了一些在大型Web应用程序中构建我的javascript代码的最佳实践/建议。

Some things that I am guilty of and want to move away from: 有些事我感到内疚,想要离开:

  1. Inline javascript in my views. 在我的视图中内联javascript。 I'm not talking masses of code but it's not uncommon for me to have some initialization code on each view. 我不是在谈论大量的代码,但我在每个视图上都有一些初始化代码并不罕见。
  2. One large "site" javascript file. 一个大的“网站”javascript文件。 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. 随着我的应用程序中使用的插件列表的增长(例如jquery ui),我的“启动”功能也正在初始化每个插件。 Often this is to avoid issue 1 (adding javascript inline). 通常这是为了避免问题1(添加javascript内联)。 Perhaps a javascript file per view is a better solution? 也许每个视图的javascript文件是更好的解决方案?
  3. 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. 我习惯于在.net中以面向对象的方式工作,但是当涉及到javascript时,这似乎就会消失。 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. 现在我知道OOP肯定是可能的,并且鼓励使用javascript,但是我可以看到一些实际的例子,说明我应该如何在真实的Web应用程序中执行它。
  4. 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命名空间的例子(凭借对象)和类似的方法,但再次可以做一些更多的背景知识。

Any sites, books etc. that people can recommend would be appreciated. 人们可以推荐的任何网站,书籍等都将受到赞赏。

I thought I'd throw in my 2 cents. 我以为我会投入2美分。

  1. Best practice suggest 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. 最佳做法建议不要在视图中使用JScript,我发现可以通过使用包含JScript的HTML帮助程序或将JScript推入JS文件并在HTML帮助程序中初始化它来避免这种情况。 This is what third party MVC control dev (eg. Telerik) uses. 这就是第三方MVC控制开发者(例如Telerik)使用的内容。 If you do decide to push the JScript into your controller, you will find it gets messy pretty quickly. 如果您决定将JScript推入控制器,您会发现它很快就会变得混乱。

  2. 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. 我经常发现自己处于每个视图都有一个JS文件的场景中,但是这可以合并为每个模块有一个JS。 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. 这也被认为是最佳做法。

  3. You will find that JScript is quite OO and this is to do with how JScript uses JSON. 您会发现JScript非常OO ,这与JScript如何使用JSON有关。 I find that the flexibility of JScript causes it to appear to lean away from OOP. 我发现JScript的灵活性使它看起来偏离了OOP。 My limited experience with JScript is to keep it short and concise and this will re-enforce re-usable JScripts. 我对JScript的有限经验是保持简洁和简洁,这将重新强制执行可重用的JScripts。

  4. I think this can be over-come with practice in JScript. 我认为这可以通过JScript实践来克服。 The biggest issue I find here is not with name-spacing, but how to avoid duplicating/double loading of JScripts. 我在这里找到的最大问题不是名称间距,而是如何避免重复/双重加载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. 如果视图是“自包含的”,理论上应该加载自己的JScripts,但这会导致JScripts被多次加载甚至产生冲突。 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). 再一次,一个好的HTML帮助器可以在这里保存你(aplogies,我没有一个很好的例子,但谷歌它,你不能出错)。

Overall, JScript is very powerful and opens up web programming to a whole new level. 总的来说,JScript非常强大,可以将Web编程提升到一个全新的水平。 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. JScript将越来越多地成为Web平台的一部分,特别是当每个人都进入HTML 5.使用MVC和JSript时,有100种不同的方法可以完成同样的事情,最佳实践只能带你到目前为止。 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. 其余由您决定,取决于您要实现的目标,目标受众是谁以及您支持的浏览器。

For me, libraries like KnockoutJS have 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. 对我来说,像KnockoutJS这样的库已经帮助组织了我的JS代码,它非常强大,我已经将它用于多个项目并且它运行得非常好。 (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) (我也是一个asp.net mvc开发人员,但这也适用于其他技术,此外,发明它的人对aspnet mvc有很多经验)

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

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