Search code examples
javascriptjquery-uibackbone.jsjquery-ui-resizable

JQuery UI resizable Resize event bind/on resize event to Backbone View


I have a view, NOT the size of the window, nor the window itself, and when it resizes, I want to compare the beginning and the ending value of the resize. JQ-UI's resize ui object however includes only the previous state, not original, so it is only grabbing the changes by pixels (albeit I think that is because I am putting the code in the resize function, and not the end function, but that isn't the real problem, as I could solve it once I know how to get the var back to the Backbone View itself). How do I get the info from within the resize back to the backbone view? self is the global window object, and this is the JQuery result from the selector of this.el.

define([ ... ], function( ... ){
  return Backbone.View.extend({
    // I also tried to use the event handlers from backbone
    events : {
      'resize' : 'info'
    },
    initialize: function(options){
      if (options) { ... }
        this.el = '#measure-rep-c55';
      }
      //Dispatch listeners
      ...
      //Binding
      this.model.bind('change', _.bind(this.render, this));
      $(this.el).on('resize', this.info);  // Here I am trying to attach the listener here according the API

      this.render();
    },
    info: function(){
      console.log('in info')
    },
    render: function(){ 
      ... //template and other stuff

      // JQ-UI resizable
      $(this.el).resizable({ 
        aspectRatio: true,
        start: function(e, ui) {
            // alert('resizing started');
        },
        resize: function( event, ui ) {
          // in here self = window
          // and this is the JQuery object
          var oldW = ui.originalSize.width;
          var newW = ui.size.width;
          var deltaWidth = newW - oldW;
          var deltaRatio = deltaWidth/oldW;
          //HOW TO SEND info (in this case var deltaRatio) back to the backbone view
          //I tried getting to the function info() so that I could access the View itself from there
        },
        stop: function(e, ui) {
            // alert('resizing stopped');
        }
      });
    },
  });
});

Solution

  • Don't create the listeners from within the resizable call, use the events hash to listen for the changes, then you have direct access to your view from the callbacks.

    events : {
      'resizestart' : 'start',
      'resizestop' : 'stop',
      'resize' : 'resize'
    },
    
    render: function(){ 
      ... //template and other stuff
    
      // JQ-UI resizable
      this.$el.resizable({ 
        aspectRatio: true
      });
    },
    
    start: function(e, ui) {
            // alert('resizing started');
    },
    resize: function( event, ui ) {
          // this is the View
          var oldW = ui.originalSize.width;
          var newW = ui.size.width;
          var deltaWidth = newW - oldW;
          var deltaRatio = deltaWidth/oldW;
     },
     stop: function(e, ui) {
        // alert('resizing stopped');
     }