How do I add a resize event to the window in a view using Backbone?

前端 未结 2 1739
日久生厌
日久生厌 2021-02-08 01:33

I have been trying to attach a handler to the resize event in one of my Backbone views. After doing some research I have discovered that you can only attach events to the view\'

2条回答
  •  醉话见心
    2021-02-08 01:57

    var BaseView = Backbone.View.extend({
    
        el: $('body'),
    
        initialize: function() {
            // bind to the namespaced (for easier unbinding) event
            // in jQuery 1.7+ use .on(...)
            $(window).bind("resize.app", _.bind(this.resize, this));
        },
    
        remove: function() {
            // unbind the namespaced event (to prevent accidentally unbinding some
            // other resize events from other code in your app
            // in jQuery 1.7+ use .off(...)
            $(window).unbind("resize.app");
    
            // don't forget to call the original remove() function
            Backbone.View.prototype.remove.call(this);
            // could also be written as:
            // this.constructor.__super__.remove.call(this);
        }, ...
    

    Don't forget to call the remove() function on the view. Never just replace the view with another one.

提交回复
热议问题