This question already has an answer here:
这个问题已经有了答案:
- Where to put view-specific javascript files in an ASP.NET MVC application? 5 answers
- 在ASP中放置特定于视图的javascript文件的位置。净MVC应用程序?5个回答
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函数。例如,单击“删除”或“插入”按钮,将调用一个操作来执行该场景背后的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代码被嵌入到
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文件放在与其视图相同的文件夹中?或者应该将它们全部放在脚本文件夹中并从那里引用?
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#代码进行分组,以便崩溃。在JavaScript中有类似的东西吗?它只是长得太容易了。
Thank you for any helpful advice.
谢谢你的建议。
1 个解决方案
#1
3
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和其他)都放在这里。我通常将内容目录用于我自己的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文件构建到一个文件中,会有很多方法)。
#1
3
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和其他)都放在这里。我通常将内容目录用于我自己的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文件构建到一个文件中,会有很多方法)。