简体   繁体   English

如何在ASP.NET MVC项目中组织前端JavaScript代码?

[英]How to organize front end JavaScript code in ASP.NET MVC project?

I have a ASP.NET MVC project which adopts lots of JavaScript (jQuery and d3). 我有一个ASP.NET MVC项目,该项目采用了许多JavaScript(jQuery和d3)。 I am new to web development, so I want to ask for some help on how to organize front end script code. 我是Web开发的新手,所以我想寻求有关如何组织前端脚本代码的帮助。

My current way is, each folder under 'Views', has only one corresponding .js file (which means all partial views (.cshtml) under that folder, share that .js file), and I explicitly initialize all .js files of my project in $(document).ready() of _Layout.cshtml. 我当前的方式是,“视图”下的每个文件夹只有一个对应的.js文件(这意味着该文件夹下的所有部分视图(.cshtml),共享该.js文件),我明确初始化了我的所有.js文件_Layout.cshtml的$(document).ready()中的项目。

This introduces 2 issues: 这引入了两个问题:

  1. Because all partial views puts there codes in one .js file, so it makes each .js file huge and complex. 因为所有局部视图都将代码放在一个.js文件中,所以这使每个.js文件又大又复杂。
  2. Because I hardcoded initialize all .js files when _Layout.cshtml is loaded, so even when one view is not loaded, its behind .js is executed, sounds not flexible. 因为我在加载_Layout.cshtml时对所有.js文件进行了硬编码初始化,所以即使未加载一个视图,也要执行其背后的.js听起来不太灵活。

So here are my questions: 所以这是我的问题:

  1. How to make each partial view has its own .js file (split current .js into small pieces). 如何使每个局部视图都有自己的.js文件(将当前.js分成小块)。
  2. How can I load and run a partial view's .js file only when that partial view is loaded. 仅在加载部分视图的.js文件时,才可以加载和运行该视图。
  3. If I use TypeScript and KnockoutJS, do they provide any benefits on organizing front end script code? 如果我使用TypeScript和KnockoutJS,它们在组织前端脚本代码方面是否有任何好处?

Thanks in advance. 提前致谢。

Split up your javascript code into multiple file such that each js file corresponds to the partial views. 将您的JavaScript代码拆分为多个文件,以便每个js文件对应于部分视图。

Then load the corresponding js in partial view in partial view instead _Layout.cshtml file. 然后在部分视图而不是_Layout.cshtml文件的部分视图中加载相应的js。 In this way, the corresponding js will execute only when the partial view is loaded. 这样,仅当加载部分视图时,相应的js才会执行。

TypeScript is a class-based object-oriented programming style applied to basic javascript, which is developed and maintained by Microsoft. TypeScript是一种基于类的,面向对象的编程风格,应用于基本javascript,由Microsoft开发和维护。 This does not help to organized the files but do help to maintain the js code in an OOP style.(link here ) 这无助于整理文件,但有助于以OOP样式维护js代码。(链接此处

KnockoutJS is Model-View-ViewModel(MVVM) pattern applied to javascript. KnockoutJS是应用于JavaScript的Model-View-ViewModel(MVVM)模式。 You can modularized each js files in KnockoutJS, ie, separate js for Model, View and ViewModel but they will have dependency each other.(see documentaion ) 您可以在KnockoutJS中对每个js文件进行模块化,即分别为Model,View和ViewModel使用js,但是它们之间会相互依赖。(请参见documentaion

There are multiple ways to skin a cat. 有多种方法可以为猫皮。 If you have javascript code specific to a partial view it's entirely fine to just skip the step of including a file and embedding JavaScript directly in there by means of the <script> tag. 如果您具有特定于部分视图的javascript代码,那么只需跳过包含文件并通过<script>标记直接将JavaScript嵌入其中的步骤就可以了。 This solves problem 1 and 2 you posted because razor partial views boil down to just arbitrary HTML inserted into the DOM when they are loaded. 这解决了您发布的问题1和2,因为剃刀局部视图可以简化为在加载时插入DOM中的任意HTML。

As for TypeScript and KnockoutJS the benefits they provide are highly subjective to yourself and the project you are working on. 至于TypeScript和KnockoutJS,它们提供的好处在很大程度上取决于您自己和您正在从事的项目。 I would suggest reading up on the features they provide and see if that's an avenue you want to pursue -- There is no right or wrong answer to this, at least not with a lot of specific context as to what you're working on. 我建议阅读他们提供的功能,看看这是否是您要追求的途径-对此没有正确或错误的答案,至少没有关于您正在从事的工作的很多特定上下文。

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

相关问题 如何在ASP.NET MVC中组织外部JavaScript文件 - How to organize external JavaScript files in ASP.NET MVC 如何同时使用AngularJS作为前端框架和ASP.NET MVC作为后端框架? - how to use AngularJS as front-end framework and ASP.NET MVC as back-end framework together? 如何使用ASP.NET MVC将JavaScript变量传递给ASP代码 - How to pass javascript variable to asp code with ASP.NET MVC 如何从ASP.NET中的前端Javascript函数调用后端C#函数 - How to call a backend C# function from front end Javascript function in ASP.NET 如何为HTML助手ASP.NET MVC编写Javascript代码? - How to write Javascript code for HTML helpers ASP.NET MVC? 是否有一个项目会自动生成JavaScript代理代码以调用ASP.NET MVC操作方法? - Is there a project that auto-generates JavaScript proxy code to call ASP.NET MVC action methods? 有关基于ASP.NET MVC2的富客户端Web应用程序的前端实现的建议 - Advice on front end implementation of rich client web app based on ASP.NET MVC2 是否有 Microsoft asp.net 和 mvc 前端脚本的文档 - Is there any documentation for Microsoft asp.net and mvc front-end scripts 在 ASP.NET CORE MVC 项目中将 Javascript 值转换为 JSON - Value Javascript to JSON in ASP.NET CORE MVC project ASP.NET&#39;InProc&#39;超时-最后执行javascript代码 - ASP.NET 'InProc' timeout - execute javascript code at the end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM