RequireJS/Backbone : inheritance of dependencies?

339 views Asked by At

Update : I've found a solution which is slighty the same as in the first answer. But in fact I'd like to know if there is a manner using RequireJS, to do that without using special var or parameters in views. Here is my solution :

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;
});

I try to use a maximum the inheritance in a Backbone project which dependencies are managed by RequireJS to avoid duplicate code. In fact, I create a new view that extend a base view. The base view has a dependency that I want to override. But even if i try to override, the original dependency is taken, instead of the new one. Note than I'm forced to inherit.

Here what I'm trying to do :

Base view I inherit :

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;
});

Then view i attempt to create. What I want is that buildAView() function takes the new dependency called AView in b-view which contains not the same source code as in 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;
});

Thanks :)

1

There are 1 answers

1
Andreas Köberle On

The problem is that AView is bound to the function it was injected, so when you call the buildAView it use the AView that was injected into the ParentView module. But there is a simple way to fix the problem. Just save AView as property of your view.

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;
});