1

我有一个视图,不是窗口的大小,也不是窗口本身,当它调整大小时,我想比较调整大小的开始值和结束值。然而,JQ-UI 的 resize ui 对象仅包含以前的状态,而不是原始状态,因此它仅按像素抓取更改(尽管我认为这是因为我将代码放在 resize 函数中,而不是 end 函数中,但是不是真正的问题,因为一旦我知道如何将 var 返回到 Backbone View 本身,我就可以解决它)。如何从调整大小返回到主干视图中的信息? self是全局window对象,this是 . 的选择器的 JQuery 结果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');
        }
      });
    },
  });
});
4

2 回答 2

5

不要从可调整大小的调用中创建侦听器,使用事件哈希来侦听更改,然后您可以从回调直接访问您的视图。

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');
 }
于 2013-08-22T21:07:03.030 回答
0

您可以使用下划线将视图的“this”绑定到事件函数,这将使您能够访问视图本身。我通常将函数体分成它们自己的函数,如下所示:

render: function() { 
  ...
  this.$el.resizable({
    aspectRatio: true,
    start: _.bind(this.didStart, this),
    resize: _.bind(this.didResize, this),
    end: _.bind(this.didEnd, this)
  });
},

didStart: function() {
  ...
},

didResize: function() {
  ...
},

didEnd: function() {
  ...
}
于 2014-03-25T19:09:16.440 回答