Sub Class a Backbone.View Sub Class & retain events

前端 未结 5 1374
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 17:29

I have a generic subclass of Backbone.View which has a close event listener.

var GenericView = Backbone.View.extend({

    events          


        
5条回答
  •  执笔经年
    2020-12-23 18:02

    Here's a solution that's worked well for me. You can use the events object in both parent and subclass without extending in it each subclass. Just extend it once in the parent class:

    APP.Views.GenericWizard = Backbone.View.extend({
      events: {
        'click .btn-prev'   : function(){}
      },
      initialize: function() {
        _(this.events).extend(APP.Views.GenericWizard.prototype.events);
      }
    });
    
    APP.Views.RegisterWizard = APP.Views.GenericWizard.extend({
      events: {
        'blur #username' : function(){}
      },
    });
    
    var registerWizard = new APP.Views.RegisterWizard();
    console.log(registerWizard.events);
    // Object {blur #username: function, click .btn-prev: function}
    

提交回复
热议问题