0

条件:

1)我们有一堆使用以下模式的遗留代码:

base.js

var Base = function(config) {
    var that={};
    that.doThing1=function(){};
    that.doThing2=function(){};
    return that;
}

child.js

var Child = function(config) {
    var that=Base;
    that.doStuff1=function(){that.doThing1();};
    that.doThing2=function(){};
    return that;
}

2)最重要的事情之一是它是遗留代码,而且很多,所以重构一切以使用不同的模式现在完全不可能

3)我正在尝试将 require.js 用于当前项目,以及 w/backbone,因此我想要在其中需要“Base”依赖项的子类。

这是我现在所在的位置,但它不起作用:

require('Base'), function(Base) {
    var Child = function(config) {
        var that=Base;
        that.doStuff1=function(){that.doThing1();};
        that.doThing2=function(){};
        return that;
    }
    return Child;
}

问题):

如何设置此模式,以便我可以将 require() 包裹在它周围,以异步获取遗留依赖项,以便我可以将 require() Child 放入主干类?

4

1 回答 1

2

好吧,这似乎做到了。让我感到震惊的是 - 请注意在 base.js 中该类的名称不存在。相反,我在 require.config() 中命名了它。

base.js

define(function() {
    return function(config) {
        var that={};
        that.doThing1=function(){};
        that.doThing2=function(){};
        return that;
    };
});

child.js

define(['Base'], function(Base) {
    return function(config) {
        var that=Base(config);
        that.doStuff1=function(){that.doThing1();};
        that.doThing2=function(){};
        return that;
    };
});
于 2012-11-07T23:29:12.370 回答