简体   繁体   English

我应该将MVC视图专用的javascript文件放在哪个文件夹中?

[英]Which folder(s) should I put my MVC view-specific javascript files in?

I was wondering what's best practice for a javascript file's location that is view-specific. 我想知道针对特定视图的javascript文件位置的最佳做法是什么。
Some of my views have 1700+ lines of code, 70% of it being javascript, so I'm putting them into a .js file to take advantage of bundling/minifying. 我的一些视图有1700余行代码,其中70%是javascript,因此我将它们放入.js文件中,以利用捆绑/最小化的优势。

I know core script files (eg. jquery) typically go into the root /Scripts folder, but there are a LOT of views that i need to do this to (50+), so I'm wondering if I should put them in their respective view folders (eg: /Views/Account/ViewName.js) or should i dump them all into the /Scripts folder at the root. 我知道核心脚本文件(例如jquery)通常进入根目录/ Scripts文件夹,但是我需要对(50+)个视图进行很多处理,因此我想知道是否应将它们放在它们的各自的视图文件夹(例如:/Views/Account/ViewName.js),还是我应该将它们全部转储到根目录的/ Scripts文件夹中。

In my practices, I've basically made the following static assets structure: 在我的实践中,我基本上做了以下静态资产结构:

public
-- css
-- javascript
---- controllers
---- lib
-- otherAssets

And using some controller logic, I add a file per controller to /public/javascript/controllers so that way my controller specific code is all bundled together. 并使用一些控制器逻辑,将每个控制器的文件添加到/public/javascript/controllers以便将控制器特定的代码全部捆绑在一起。

You could just as easily rig it to be per model, and then have object oriented methods in your js files like this: 您可以轻松地将其绑定到每个模型,然后在js文件中使用面向对象的方法,如下所示:

myModel = {
    pageA: function() {
        //stuff
    },
    pageB: function() {
        //stuff
    }
};

doing it this way would mean that you'd need a parent javascript file that basically finds the proper javascript model object (ie myModel ), then controller method ( pageA ) and then calls that function. 以这种方式进行操作将意味着您需要一个父javascript文件,该文件基本上会找到正确的javascript模型对象(即myModel ),然后是控制器方法( pageA ),然后调用该函数。

Anything more complex than that really starts bringing you into the territory of you needing a javascript assets manager like require.js 比这更复杂的事情真正开始使您进入需要像require.js这样的javascript资产管理器的领域

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

相关问题 设置特定于视图的maxAllowedContentLength - Setting view-specific maxAllowedContentLength 我应该在哪个层放置我的存储库? - In which layer should i put my repository? WPF MVVM,我应该在哪里放置视图特定的属性? - WPF MVVM, where should I put view specific properties? ASP.Net MVC Pager助手和视图无法一起使用-我应该在视图的第一行中添加什么 - ASP.Net MVC Pager helper and view not working together - what should I put in my views first line 我可以在MVC Project中捆绑和缩小我的应用文件夹的js文件吗? - Can i bundle and minify my app folder's js files in MVC Project? 将元素放入数组,然后可以传递到我的MVC部分视图 - Put elements into an array that I can then pass to my MVC partial view 我如何在MVC4中以varbinary(max)数据类型在sql server中的视图中显示doc,docx和pdf文件的内容 - How can i show doc,docx and pdf files's content in view in mvc4 which was in sql server as varbinary(max) datatype MVC放置视图组件文件的位置 - MVC Where To Put View Component Files 我应该在WPF中将应用程序特定的配置文件存储在哪里 - Where should I store my App specific config files in WPF 我应该将Windsor安装程序放在哪个项目中? - In which project should I put the Windsor installers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM