0

I have this mixin that is used by many views

var EditTableRowMixin = {
   events: {
     "click .save": "save",
     "click .abort": "abort"
   },

   /* common methods for table rows */
};

Now I have a new view that needs a couple more events than this mixin has:

var SpecialRowEditView = Backbone.View.extend({
    tagName : "div",

    events: {
        "click .remove" : "remove",
        "click .add" : "add"
    },
    initialize: function(){
       /*Do things */
    },
    ...
    ...
    ...            
  });
_.extend(SpecialRowEditView.prototype, EditTableRowMixin);
return SpecialRowEditView;

I have tried to just add these events to this.events (which is the mixins events). It works, but breaks all the other views using the mixin because those now look for the save and abort functions for some reason.

How can I extend the mixin's events with the ones I need for this view only?

4

1 に答える 1

0
var more_events = {
        events: {
          "click .remove" : "remove",
          "click .add" : "add"
        }
    },

_.extend(SpecialRowEditView.prototype, _.merge(EditTableRowMixin, more_events));

あなたのイベントは mixin によって上書きされます。それらを一緒にマージする必要があります。ここでは、アンダースコアの代わりにlodashを使用しています。

于 2013-11-12T09:01:35.900 に答える