繁体   English   中英

合并Mixins和Views bone.js的事件定义

[英]Merging Event definitions for mixins and Views backbone.js

我为主干创建了一个mixin,我想知道是否有比此更好的方法来合并事件哈希。

混合:

app.mixin.filter = {
events: {
    'click .label': 'toggle',
    'keyup .file-search': 'updateSearchFilter'
},
 //more stuff
}

视图:

app.DashboardView = Backbone.View.extend({

    el: '.contentwrap',

    dashEvents: {'click .project-btn': 'addProjectModal'},

    initialize: function() {

    //other stuff

    _.defaults(app.DashboardView.prototype.events, this.dashEvents);

    //other stuff

    }
}

 _.extend(app.DashboardView.prototype, app.mixin.filter);

我对调用事件哈希dashEvents特别不满意。 有什么办法可以将事件保留为“事件”? 还是有一种标准模式来处理此类问题?

app.DashboardView = Backbone.View.extend({
  el: '.contentwrap',
  events : _.extend({}, app.mixin.filters.events, {
    'click .project-btn': 'addProjectModal'
  })
}

我一直在我的应用程序中这样做:

app.DashboardView = Backbone.View.extend(_.defaults({
    el: '.contentwrap',

    events: _.defaults({ 
        'click .project-btn': 'addProjectModal'
    }, app.mixin.filter.events)

}, app.mixin.filter));

在这里,我想出了稍微不同的方法:

app.mixin.filter = function () {
  _.defaults(this.events, {
    'click .label': 'toggle',
    'keyup .file-search': 'updateSearchFilter'
  });
  _.defaults(this, {
    // more stuff
  });
}

app.DashboardView = Backbone.View.extend({
    el: '.contentwrap',
    events: {'click .project-btn': 'addProjectModal'}
}

app.mixin.filter.call(app.DashboardView.prototype);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM