Inheritance RequireJS/主干:依赖项的继承?

Inheritance RequireJS/主干:依赖项的继承?,inheritance,backbone.js,requirejs,dependency-management,resthub,Inheritance,Backbone.js,Requirejs,Dependency Management,Resthub,更新:我找到了一个与第一个答案稍有相同的解决方案但事实上,我想知道是否有一种使用RequireJS的方式,在视图中不使用特殊的var或参数。以下是我的解决方案: define(['resthub', 'backbone', 'views/myModule/parent-view', 'views/myModule/a-view'], function(Resthub, Backbone, ParentView, AView) { var ParentView = Backbone.Vi

更新:我找到了一个与第一个答案稍有相同的解决方案但事实上,我想知道是否有一种使用RequireJS的方式,在视图中不使用特殊的var或参数。以下是我的解决方案:

define(['resthub', 'backbone', 'views/myModule/parent-view', 'views/myModule/a-view'],
function(Resthub, Backbone, ParentView, AView) {

    var ParentView = Backbone.View.extend({

        // Stuff before

        buildAView : function(aViewObject){
            var aView = aViewObject || AView;

            // Enought source code before and after those following lines
            // that I don't want to duplicate in ChildView
            this.aView = new aView();
            this.aView.render();
        }

        // Stuff after

    });

    return ParentView;
});

我尝试在主干项目中使用最大的继承性,其中依赖项由RequireJS管理,以避免重复代码。 事实上,我创建了一个扩展基础视图的新视图。基本视图有一个我要覆盖的依赖项。但即使我尝试重写,也会使用原始依赖项,而不是新的依赖项。比我被迫继承的还要多

下面是我想做的:

我继承的基本视图:

define(['resthub', 'backbone', 'views/myModule/parent-view', 'views/myModule/a-view'],
function(Resthub, Backbone, ParentView, AView) {

    var ParentView = Backbone.View.extend({

        // Stuff before

        buildAView : function(){
            // Enought source code before and after those following lines
            // that I don't want to duplicate in ChildView
            this.aView = new AView();
            this.aView.render();
        }

        // Stuff after

    });

    return ParentView;
});
然后是我试图创建的视图。我想要的是buildAView()函数在b-view中接受名为AView的新依赖项,该依赖项包含与a-view中不同的源代码

define(['resthub', 'backbone', 'views/myModule/parent-view', 'views/myModule/b-view'],
function(Resthub, Backbone, ParentView, AView) {

    var ChildView = ParentView.extend({

        // Stuff before

        render: function() {

            ParentView.__super__.render.apply(this, []);

            /* some stuff inbetween*/

            this.buildAView();
        }

    });

    return ChildView;
});

谢谢:)

问题在于
AView
绑定到它所注入的函数,因此当您调用
buildAView
时,它使用注入到
ParentView
模块的
AView
。但是有一个简单的方法来解决这个问题。只需将
AView
另存为视图的属性即可

define(['resthub', 'backbone', 'views/myModule/parent-view', 'views/myModule/a-view'],
function(Resthub, Backbone, ParentView, AView) {

    var ParentView = Backbone.View.extend({

        // Stuff before
        AView: AView,

        buildAView : function(){
            // Enought source code before and after those following lines
            // that I don't want to duplicate in ChildView
            this.aView = new this.AView();
            this.aView.render();
        }

        // Stuff after

    });

    return ParentView;
});

define(['resthub', 'backbone', 'views/myModule/parent-view', 'views/myModule/b-view'],
function(Resthub, Backbone, ParentView, AView) {

    var ChildView = ParentView.extend({

        // Stuff before
        AView: AView,

        render: function() {

            ParentView.__super__.render.apply(this, []);

            /* some stuff inbetween*/

            this.buildAView();
        }

    });

    return ChildView;
});

我发现这是一个范围问题。事实上,我想知道是否有另一种方法,使用RequireJS,在没有包含AView的var的情况下实现这一点?从我的回答开始,我一直在以与您建议的类似的方式工作:buildAView函数可以接收一个参数,如果在ParentView中给定,则该参数将替代AView对象,否则,将使用ParentView的define中的原始AView。看我原来的帖子。