简体   繁体   English

有没有办法缩短Require.js的require语句? 我误解了如何使用它吗?

[英]Is there a way to shorten Require.js's require statement? Am I misunderstanding how to use it?

I've started using require.js for my website, but one thing that bugs me is this massive require statement at the top of my files. 我已经开始在我的网站上使用require.js,但有一件事让我感到困惑的是我文件顶部的大量需求声明。

require(["hype", "jquery", "knockout", "environment","bootstrap", "datamodel", "datatable", "datatables", "dataTablesBootstrap", "utils", "notifications", "moment", "datatablesFilterDelay"],
function(hype, $, ko, env, bootstrap, datamodel, datatable, datatables, dataTablesBootstrap, utils, not, moment, dfd) {

I'm not sure if I'm misunderstanding how I'm supposed to use require.js. 我不确定我是否误解了我应该如何使用require.js。 Each of my pages have their respective javascript files with a require statement like this at the top. 我的每个页面都有各自的javascript文件,顶部有一个这样的require语句。

I could probably shorten it by refactoring everything that I'm including into a single module and just require that since many pages rely on similar things (most need hype, jquery, knockout, bootstrap, etc), but I'm not sure if it's the best practice thing to do. 我可以通过将我所包含的所有内容重构为单个模块来缩短它,并且只需要因为许多页面依赖于类似的东西(大多数需要炒作,jquery,淘汰赛,引导程序等),但我不确定它是否是最好的做法。 Any ideas? 有任何想法吗?

Having lots of dependencies is against Single Responsibility Principle of SOLID Principles. 拥有大量依赖性是针对SOLID原则的单一责任原则。

The more dependencies our class takes in, the higher its efferent coupling to other classes, meaning that it knows a little bit too much. 我们的类所依赖的依赖关系越多,它与其他类的传出耦合就越高,这意味着它知道的太多了。 If it knows too much, it is most likely because it is also doing too much. 如果它知道太多,很可能是因为它也做得太多了。 The class is no longer focused on one thing. 班级不再专注于一件事。

This link is about dealing too many dependencies problem when Injection and C# is used, but I believe it is right for any language. 这个链接是关于在使用Injection和C#时处理太多依赖项问题,但我相信它适用于任何语言。

Although, if you do not want to to re-factor you code, there is a risk of mismatched dependency names with named function arguments, and it can start to look a bit strange if your module has many dependencies. 虽然,如果您不想重新考虑代码,则存在使用命名函数参数的依赖项名称不匹配的风险,如果您的模块具有许多依赖项,它可能会开始显得有些奇怪。 http://requirejs.org/docs/whyamd.html#sugar contains some suggestion how to make your code easier. http://requirejs.org/docs/whyamd.html#sugar包含一些如何使代码更容易的建议。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM