net mvc開發人員的javascript最佳實踐 - javascript best practices for asp.net mvc developers -开发者知识库

net mvc開發人員的javascript最佳實踐 - javascript best practices for asp.net mvc developers -开发者知识库,第1张

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:

有些事情讓我感到內疚,我想離開:

  1. 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.
  2. 在我的視圖中內聯javascript。我說的不是大量的代碼,但是在每個視圖上都有一些初始化代碼是很常見的。
  3. 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?
  4. 一個大的“站點”javascript文件。正如我的應用程序中使用的插件列表(例如jquery ui)一樣,我的“啟動”函數也在初始化每個插件。這通常是為了避免問題1(添加javascript內聯)。也許每個視圖都有一個javascript文件是更好的解決方案?
  5. 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.
  6. 一切似乎很程序化。我習慣了在。net中以面向對象的方式工作,但是當涉及到javascript時,這似乎就超出了窗口。現在我知道了OOP當然是可能的,並且受到了javascript的鼓勵,但是我可以看到一些實際的例子,說明我應該如何在真實的web應用程序上下文中進行操作。
  7. 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.
  8. 命名空間/項目結構——我已經看到了一些javascript命名空間的例子(借助對象),也喜歡這種方法,但同樣需要更多的背景知識。

Any sites, books etc. that people can recommend would be appreciated.

任何人們可以推薦的網站、書籍等都會受到歡迎。

3 个解决方案

#1


9  

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. 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.

    最佳實踐建議不要在視圖中使用JScript,我發現可以通過使用包含JScript或將JScript插入JS文件並在HTML helper中初始化它的HTML helper來避免這種情況。這就是第三方MVC控件開發(例如。Telerik)使用。如果您決定將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. 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.

    我經常發現自己在相同的場景中,每個視圖都有一個JS文件,但是可以合並到每個模塊有一個JS文件。這同樣取決於代碼的復雜程度。如果發現許多視圖具有相同的初始化,那么可以使用基本視圖方法。這也被認為是最佳實踐。

  3. You will find that JScript is quite OO and 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.

    您將發現JScript是相當面向對象的,這與JScript如何使用JSON有關。我發現JScript的靈活性使得它看起來更傾向於OOP。我對JScript有限的經驗是保持它的簡短,這將加強可重用的JScripts。

  4. 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中通過實踐來克服。我在這里發現的最大問題不是名稱空間,而是如何避免重復/重復加載JScripts。如果視圖是“自包含的”,那么理論上應該加載它們自己的JScripts,但這將導致多次加載JScripts甚至產生沖突。同樣,一個好的HTML助手可以將您保存在這里(aplogies,我沒有很好的示例,但是谷歌,您不會出錯)。

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種完全相同的方法,而最佳實踐只能帶您到此為止。剩下的問題取決於你想要實現什么,你的目標用戶是誰,你支持什么瀏覽器。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复