Backbone View Inheritance

前端 未结 4 1945
猫巷女王i
猫巷女王i 2021-01-12 20:33

I am trying to write a Backbone view for an object browser which is designed to be implemented in several places with different object types and slightly different operation

相关标签:
4条回答
  • 2021-01-12 21:09

    I think I've figured out the answer to my own problem.

    I believe the right way to achieve what I am looking for is to move the initialization of properties in to the initialize method provided by Backbone views. This way they are initialized

    var BrowserView = Backbone.View.extend({
        initialize: function () {
            this.collections = [];
        }
    });
    
    var FileBrowserView = BrowserView.extend({
        initialize: function () {
            BrowserView.prototype.initialize.apply(this);
            
            this.collections.push({name: 'Example Collection' + Math.rand()});
        }
    });
    
    
    var FileBrowserInstance1 = new FileBrowserView;
    console.log(FileBrowserInstance1.collections);
    
    var FileBrowserInstance2 = new FileBrowserView;
    console.log(FileBrowserInstance2.collections);
    

    http://jsfiddle.net/yssAT/2/

    0 讨论(0)
  • 2021-01-12 21:12

    It's hard to see what exactly your goal is.

    but this is how i see it if you have an view object

    var myView = Backbone.View.extend({
        foo: "bar"
    });
    

    and you have it extend the backbone.View... then you actually have a new view object with everything of backbone.view, and the extra options you give as parameters.

    if you then go and create a second view, that extends your first one it will get everything from your first view, + it's own extras

    var mySecondView = myView.extend({
        foobar: "f00b@r"
    });
    

    if you would create an instance of the second view and log it's foo property it will still hold "bar" as value

    var mySecondViewInstance = new mySecondView();
    console.log("mySecondViewInstance.foo: ", mySecondViewInstance.foo);
    console.log("mySecondViewInstance.foobar: ", mySecondViewInstance.foobar);
    

    if i create a new instance of my first view, and change foo into "changed-foo" the log of foo on mySecondViewInstance will still be "bar"

    var myViewInstance = new myView();
    myViewInstance.foo = "changed-foo";
    console.log("mySecondViewInstance.foo: ", mySecondViewInstance.foo);
    console.log("mySecondViewInstance.foobar: ", mySecondViewInstance.foobar);
    

    a JS-Fiddle to play around with it can be found here: http://jsfiddle.net/saelfaer/uNBSW/

    0 讨论(0)
  • 2021-01-12 21:20

    Inherit from Backbone.View doesn't work, or is quite complex.

    You should create a common object, which every of your view will inherit from, ie :

    var ViewInterface = {
      events        : { /* ... */ },
      initialize    : function (options) { /* ... */ },
      otherFunction : function (options) { /* ... */ },
    }
    

    each of your view would extend from this object :

    var BrowserView = Backbone.View.extend(_.extend(ViewInterface, {
      anotherFunction : function (options) { /* ... */ },
    })
    
    var AnotherView = Backbone.View.extend(_.extend(ViewInterface, {
      yetAnotherFunction : function (options) { /* ... */ },
    })
    
    0 讨论(0)
  • 2021-01-12 21:21

    This gist shows a better alternative: https://gist.github.com/2287018

    0 讨论(0)
提交回复
热议问题