requireJS:如何为整个网站构建 Javascript 结构?
我有 3000 多行 javascript,我需要将它们变成一个合理/可维护的结构。我选择使用 requireJS 因为它已经被一些人推荐给我了。我有一堆在整个应用程序中使用的变量,并且需要在任何地方都可用。我还有一堆需要随处可用的功能。除了这两个依赖项之外,大多数代码都可以分为自己的模块。
我无法理解如何管理我的主要变量,这样如果一个代码模块对变量进行更改,其余的 JS 模块就会看到该变化。我想我需要看一些示例演示了 requireJS 如何在比文档中的示例更大规模的情况下工作。
如果有人是经验丰富的 requireJS 用户,我很乐意听到您的提示!
I have 3000+ lines of javascript that I need to get into a sensible/maintainable structure. I have chosen to use requireJS as it has been recommend to me by a few people. I have a bunch of variables that are used throughout the application and need to be available everywhere. I also have a bunch of functions that need to be available everywhere. Apart from these two dependencies most of the code can be divided off into their own modules.
I am having trouble understanding how to manage my main variables so that if one module of code makes changes to the variables the rest of the JS modules will see that change. I think I need to see a few examples that demonstrate how requireJS is intended to work on a larger scale that the examples in the documentation.
If anyone is an experienced requireJS user I would love the hear your tips!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
RequireJS 的重点是避免对这些全局变量和全局函数的需要。
您不能将这些全局变量包装到一个模块中,然后在其他模块中依赖它吗?
例如,一个 RequireJS 模块化 Dojo 可能类似于:
The whole point of RequireJS is to avoid the need for these global variables and global functions.
Can't you wrap those globals into a module, then depend on it in your other modules?
For example, a RequireJS modularized Dojo may be something like:
RequireJS 为您提供了更好的封装模块选项,但它根本不会改变 Javascript 本身。作为过渡策略,您仍然可以在功能块内定义全局变量。包含这些定义的依赖模块将确保它在依赖模块之前运行。
下一步是将这些方法分配给除 window 之外的对象,然后通过从 RequireJS 模块依赖项接收的变量使用该对象。
希望当您完成此操作时,您可能会对更清洁的解决方案有所了解。我将(现在仍然是)一个单文件项目重构为多个文件,包括可选插件,尽管我在将 RequireJS 添加到混合中之前完成了大部分工作。
RequireJS gives you better options for encapsulating modules, but it doesn't change Javascript itself at all. As a transition strategy, you can still define your globals inside the function block. Depending on the module that contains these definitions will ensure that it has run before the dependent modules.
Next step would be to assign those methods to an object other than window, and then use that object through the variable received from RequireJS module dependency.
Hopefully by the time you've done this, you might have some insight into a cleaner solution. I refactored (and still am) a single-file project into several files, including optional plug-ins, although I did most of it before adding RequireJS to the mix.
请参阅 RequireJS 文档:
所以我认为您可以像所有其他模块一样定义()您的主模块并使子模块依赖于该主模块。然后模块对象作为参数传递给子模块的定义函数。您不必使用全局变量。
See the RequireJS documentation:
So I think you can define() your main module like all other modules and make the submodules depend on that main module. Then the module object is passed as an argument to the definition function of a submodule. You don't have to use global variables.
如果要在模块之间共享信息,请将信息附加到模块对象,并让其他模块依赖该模块,并检查其属性。
如果您有现有代码,则可以在清理时分配给
window.x
以提供全局x
。If you want to share information between modules, attach the information to the module object, and have the other modules rely on that module, and check its property.
If you have existing code, you can assign to
window.x
to provide a globalx
while you are cleaning it up.