【问题标题】:why some events do not work in backbone为什么有些事件在主干中不起作用
【发布时间】:2014-06-01 09:05:15
【问题描述】:

在事件哈希中,我想使用调整大小事件,但它们 也不工作。但是,单击事件效果很好。为什么调整大小不起作用?

var SubheaderView = Backbone.View.extend({

    id: 'gallery',
    tagName: 'div',

    events: {
        'click #minor': 'getPadding',
        'resize #minor': 'getPadding' //not work
    },

    initialize: function() {
        this.render();
    },

    render: function() {
        this.$el.append(subheaderTemplate);
    },

    getPadding: function() {
        var pad_top = Math.floor($('#minor').height() * 0.4);
        var pad_left = Math.floor($('#minor').width() * 0.07);
        var pad = pad_top + 'px 0px 0px ' + pad_left + 'px';

        $('#cover h1').css({'padding': pad});
    }
});

【问题讨论】:

标签: javascript backbone.js


【解决方案1】:

它根本与Backbone 无关。根据MDN,只有windowresize 事件。

【讨论】:

    【解决方案2】:

    你可以这样做: 将此代码添加到您正在使用的主干.js 文件中

    Backbone.View.prototype.eventAggregator = _.extend({}, Backbone.Events);
      $(window).resize(function () {
      Backbone.View.prototype.eventAggregator.trigger('window:resize');
    });
    

    然后将事件聚合器绑定到任何想要使用resize事件的视图

    this.eventAggregator.bind('window:resize',this.resize)
    

    其中this.resize 是为调整大小事件调用的函数

    【讨论】:

      猜你喜欢
      • 2014-10-31
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2015-07-24
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多