Backbone View: Inherit and extend events from parent

前端 未结 15 797
醉梦人生
醉梦人生 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:28

    For Backbone version 1.2.3, __super__ works fine, and may even be chained. E.g.:

    // A_View.js
    var a_view = B_View.extend({
        // ...
        events: function(){
            return _.extend({}, a_view.__super__.events.call(this), { // Function - call it
                "click .a_foo": "a_bar",
            });
        }
        // ...
    });
    
    // B_View.js
    var b_view = C_View.extend({
        // ...
        events: function(){
            return _.extend({}, b_view.__super__.events, { // Object refence
                "click .b_foo": "b_bar",
            });
        }
        // ...
    });
    
    // C_View.js
    var c_view = Backbone.View.extend({
        // ...
        events: {
            "click .c_foo": "c_bar",
        }
        // ...
    });
    

    ... which - in A_View.js - will result in:

    events: {
        "click .a_foo": "a_bar",
        "click .b_foo": "b_bar",
        "click .c_foo": "c_bar",
    }
    

提交回复
热议问题