将RequireJS与遗留代码一起使用 [英] Using RequireJS with legacy code

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

问题描述

我正在使用一个非常大的项目,该项目使用:

I'm working with a very large project that uses:


  1. 包含带脚本标记的javascript文件的旧版JSP页面

  2. 使用其他没有RequireJS的javascript模块的骨干模型和视图

我们现在想要开始使用RequireJS jQuery,BackboneJS和UnderscoreJS用于我们从现在开发的所有内容,但是我们没有资源来重写所有遗留的JSP页面。我们可能有时间重写我们已经开发的Backbone模型和视图。

We now want to start using RequireJS with jQuery, BackboneJS and UnderscoreJS for everything we develop from now on, but we don't have the resources to rewrite all the legacy JSP pages. We may have time to rewrite the Backbone models and views we have already developed.

问题是,对于我们的遗留代码(上面的1和2),我们包括了所有javascript文件在一个巨大的文件中并发送到浏览器。这个庞大的文件必须能够与我们新的RequireJS模板共存,但我怎样才能将巨大文件的某些部分分开,以便我可以使用RequireJS将它们与模板一起使用?无需重写使用该部分文件或具有重复代码的所有页面。

The problem is that for our legacy code (both 1 and 2 above) we include all our javascript files in a huge file and ship to the browsers. This huge file must be able to co-exist with our new RequireJS templates, but how can I e.g. separate certain parts of the huge file, so I can use them with the templates using RequireJS? Without having to rewrite all pages that uses this part of the file, or having duplicate code.

谢谢!

推荐答案

我不知道我是否完全掌握了手头的问题,但我认为 shim map RequireJS的功能将帮助你。

I don't know if I fully grasp the problem at hand, but I think a the shim or map functions of RequireJS will help you out.

从巨大的javascript文件中提取你想要的新模块中的部分。然后告诉RequireJS你的巨大的javascript文件是使用shim这个新模块的依赖。类似于:

Extract the parts you want in a new module from your huge javascript file. Then tell RequireJS that your huge javascript file is a dependecy for this new module using shim. Something like:

requirejs.config({
    shim: {
        'new-module': {
            deps: ['huge-javascript-file'],
            exports: 'NewModule'
    }
});

Shim文档: http://requirejs.org/docs/api.html#config-shim

地图功能当只有部分新代码必须使用旧的大文件时,它可能会很有用。查看此文档: http://requirejs.org/docs/api.html#config-地图

The map function might be useful when only portions of your new code have to use your old huge file. Check out this documentation: http://requirejs.org/docs/api.html#config-map

这篇关于将RequireJS与遗留代码一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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