骨干视图覆盖父事件功能

时间:2015-06-30 01:37:42

标签: backbone.js

是否可以在子视图中覆盖closeClick功能?单击.close时,只运行子级closeClick函数而不是父级。

var ParentView = Backbone.View.extend({
 events:{
    "click .close": "closeClick"
 },

 closeClick: function(){
    //do parent stuff
 }
});

var ChildView = ParentView.extendable.extend({
  events:{
    "click .close": "closeClick",
    //more child events
  },

  closeClick: function(){
     //only do child stuff not parent when .close is clicked
  }
});

1 个答案:

答案 0 :(得分:1)

您所写的内容可以正常使用正常的Backbone .extend。不知道这个奇怪的extendable陈述是什么 - 这可能是打破它的原因。

var ParentView = Backbone.View.extend({
 events: {
    "click .close": "closeClick"
 },

 closeClick: function(){
    //do parent stuff
 }
});

var ChildView = ParentView.extend({
  events: function() {
    var parentEvents = _.result(ParentView.prototype, 'events');
    return _.extend({
        'keyup input': 'someOtherChildEvent'
    }, parentEvents);
  },

  closeClick: function() {
     //only do child stuff not parent when .close is clicked
  }
});