Backbone View Inheritance

前端 未结 4 1965
猫巷女王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/

提交回复
热议问题