如何在 ASP.NET MVC 应用程序中组织 JavaScript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10399122/
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
How to organize the JavaScript Code in ASP.NET MVC applications
提问by Blaise
We are to add some Ajax functions on a view page. Say, click a delete or insert button and an Action will be called to perform a CRUD job behind the scene and then refresh the items list after that operation.
我们将在视图页面上添加一些 Ajax 函数。比如说,单击删除或插入按钮,将调用一个 Action 以在后台执行 CRUD 作业,然后在该操作后刷新项目列表。
In many existing examples, the javascript code is embeded in the view page within the <script>
tag. Since there are usually many functions that one view may need. Say, when insert a record, there will be function to collect the form data, validate the data (this part can be very long), and call the action by $.ajax()
. For this reason, we are thinking about place all those javascript into a separate .js
file.
在许多现有示例中,javascript 代码嵌入在<script>
标签内的视图页面中。因为通常一个视图可能需要许多功能。比如说,在插入一条记录时,会有一个函数来收集表单数据,验证数据(这部分可能很长),并通过 调用动作$.ajax()
。出于这个原因,我们正在考虑将所有这些 javascript 放入一个单独的.js
文件中。
So, what is the professional practice in this? Should we place the .js
files under the same folder with its Views
? Or should be place all of them into the Script
folder and reference from there?
那么,这其中的专业做法是什么?我们应该将.js
文件与其放在同一文件夹下Views
吗?还是应该将它们全部放入Script
文件夹并从那里引用?
Should we create seperate .js
files for each individual view or combine all the .js
files for different views into a large file?
我们应该.js
为每个单独的视图创建单独的文件还是将.js
不同视图的所有文件组合成一个大文件?
Also, we love how we can group C# code using #region
so as to collapse. Is there anything similar in JavaScript? It just grows lengthy too easily.
此外,我们喜欢如何使用 C# 代码进行分组#region
以便折叠。JavaScript 中有类似的东西吗?它只是太容易变长了。
Thank you for any helpful advice.
感谢您提供任何有用的建议。
回答by Styxxy
I wouldn't use the Scripts directory for this purpose, because all default JavaScript files (i.e. jQuery and others) are placed here. I generally use the Content directory for my own JavaScript (also CSS and images) files.
我不会为此使用 Scripts 目录,因为所有默认的 JavaScript 文件(即 jQuery 和其他文件)都放置在这里。我通常将 Content 目录用于我自己的 JavaScript(还有 CSS 和图像)文件。
You can structure your files in seperate folders like your views (i.e. ~/Content/js/myviewfolder/somename.js).
您可以在单独的文件夹中构建您的文件,例如您的视图(即 ~/Content/js/myviewfolder/somename.js)。
If you want to structure your JavaScript code: you can seperate independant code into different files and you can let it process to one file. Or you can group it into an anonymous self executing code blocks, you can then collapse those blocks. Apart from that, I am out of ideas. (Personally, I would go for different files (there are ways if building different JavaScript files into one file.)
如果你想构建你的 JavaScript 代码:你可以将独立的代码分离到不同的文件中,你可以让它处理到一个文件中。或者您可以将其分组为匿名的自执行代码块,然后您可以折叠这些块。除此之外,我没有想法。(就我个人而言,我会选择不同的文件(如果将不同的 JavaScript 文件构建到一个文件中,则有多种方法。)