将此应用于jQuery回调

时间:2013-11-15 18:20:18

标签: javascript jquery twitter-bootstrap

我想在引导模态关闭时触发某些东西。有一个倾听者:modal.on('hidden', function(){ ... })
我的问题是我想要更改我只能使用this引用的对象的值,但在模态回调中,this指的是模态。以下是我的代码的外观:

return {
 bool: false,

 openModal: function(modal) {
  modal.open();

  // this part doesn't work because this refers to the jQ modal object instead of the current object
  modal.on('hidden', function() { this.bool = true; });
 }
}

1 个答案:

答案 0 :(得分:3)

openModal: function(modal) {
  modal.open();

  var that = this;
  modal.on('hidden', function() { that.bool = true; });
 }