[英]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.