RequireJS和遗留应用程序 [英] RequireJS and legacy application

查看:166
本文介绍了RequireJS和遗留应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遗留应用程序,我已经重构应用到单独backbone.marionette应用程序的一部分。我没有时间或预算来重构整个事情,我想我的code更容易管理,这使我想起requirejs的。

I have a legacy application and I have refactored parts of the application into separate backbone.marionette applications. I do not have the time or the budget to refactor the whole thing and I want my code to be easier to manage which made me think of requirejs.

大多数文件都精缩和被改写的在一起。

Most of the files are minified and munged together.

我可以使用requirejs这种类型的混合解决方案,我可以在单独的骨干模块工作,并仍可以访问现有的JavaScript?

Can I use requirejs for this type of hybrid solution where I can work on separate backbone modules and still access the existing javascript?

推荐答案

正如有人谁只是一个传统的使用Require.js最近开始,骨干网,使用codeBase的,我觉得你的痛苦:-)我用了一个组合办法,我将在这里列出。

As someone who just recently started using Require.js on a legacy, Backbone-using codebase, I feel your pain :-) I used a combination of approaches, which I'll list here.

让我们说你有fileA.js和fileB.js,你要转换fileB.js使用要求,在不改变fileA.js:

Let's say you have fileA.js and fileB.js, and you want to convert fileB.js to use Require, without changing fileA.js:


  • 滥用全局空间

  • Abuse the global space

需要不强迫你通过它导入每个变量;即使是在要求,指明分数文件,您仍然可以访问全局变量,你会与非要求,指明分数code以同样的方式。这意味着,如果的fileA创建所有其在全球/窗口命名变量(如果你没有使用之前需要,这是非常有可能)的,FILEB可以访问它们的fileA是否使用要求。

Require doesn't force you to import every variable through it; even in a Require-ified file, you can still access global variables the same way you would with non-Require-ified code. This means that if fileA creates all of its variables in the global/window namespace (which is very likely if you weren't using Require before), fileB can access them whether or not fileA uses Require.

这清盘是我的大部分我的旧文件的解决方案;我只是把它们作为IS,并把它们下面所有的新要求,指明分数的东西。这样,每个全局他们创造的准备和等待时要求,指明分数文件需要他们。

This wound up being my solution for most of my legacy files; I just left them as is, and put all the new Require-ified stuff below them. That way every global they create is ready and waiting when the Require-ified files need them.

现在,这是伟大的,如果FILEB依赖的fileA,但如果它的愿望相反?好了,还要求并不prevent您进行新的全局变量,这意味着FILEB可以分享任何东西它的fileA想,只要它愿意把它放在全局空间。

Now, this is great if fileB depends on fileA, but what if it's the reverse? Well, Require also doesn't prevent you from making new global variables, which means that fileB can share anything it wants with fileA, as long as it is willing to put it in the global space.

复制code

不要生气;我知道编码实践是多么重要干是。然而,仅仅几个文件我清盘做正在作出要求,指明分数重复。这清盘是必要的,因为我使用的一个把手插件的要求做我的模板编译,所以如果我想的任何文件车把我需要它是要求,指明分数来使用。

Don't get upset; I know how important "DRY" coding practices are. However, for just a few files what I wound up doing was making Require-ified duplicates. This wound up being necessary because I'm using a Handlebars plug-in for Require to do my template compiling, so if I wanted any file to use Handlebars I needed it to be Require-ified.

要打击正常的未干的问题,我添加了注释到旧的文件有效地说:不添加任何东西到这个文件中,要求,指明分数的版本是真正的版本。我的计划是慢慢把更多的网站来要求随着时间的推移,直到我终于可以消除原有的,过时的文件。我们有一个小商店,所以它适用于我们,但在更大的公司,这可能飞不起来。

To combat the normal un-DRY issues, I added comments to the old files effectively saying "don't add anything to this file, the Require-ified version is the 'real' version". My plan is to slowly convert more of the site to Require over time, until I can finally eliminate the original, obsolete file. We have a small shop, so it works for us, but at a larger company this might not fly.

重构

我知道,你说你想避免这种情况,但有时有点重构可以给你很多爆炸为您的降压。我个人几乎重构在所有的东西,但也有只是一对夫妇的地方是一个小的调整,大大简化了问题。

I know, you said you wanted to avoid this, but sometimes a little refactoring can give you a lot of bang for your buck. I personally barely refactored anything at all, but there were just a couple places were a small tweak greatly simplified matters.

总的来说,我看到重构的东西切换到要求(以随时间慢慢把你非要求,指明分数code中对折)后,你怎么做。

Overall I see refactoring as something you do after you switch to Require (to slowly over time bring your non-Require-ified code "in to the fold").

垫片

Chchrist是正确地说,垫片解决中途要求的问题,但我个人不使用它们所有的好办法,所以我真的不能说太多关于他们,除了看在给他们,他们可能会有所帮助。

Chchrist is correct in saying that shims are a good way to solve the "half-way to Require" issues However, I personally didn't use them at all, so I can't really say much about them except "look in to them, they'll probably be helpful".

这篇关于RequireJS和遗留应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆