Backbone View: Inherit and extend events from parent

前端 未结 15 736
醉梦人生
醉梦人生 2020-11-30 16:50

Backbone\'s documentation states:

The events property may also be defined as a function that returns an events hash, to make it easier to programmatic

15条回答
  •  攒了一身酷
    2020-11-30 17:18

    If you are sure that the ParentView has the events defined as object and you don't need to define events dynamically in ChildView it is possible to simplify soldier.moth's answer further by getting rid of the function and using _.extend directly:

    var ParentView = Backbone.View.extend({
        events: {
            'click': 'onclick'
        }
    });
    
    var ChildView = ParentView.extend({
        events: _.extend({}, ParentView.prototype.events, {
            'click' : 'onclickChild'
        })
    });
    

提交回复
热议问题