对象内的Javascript回调

对象内的Javascript回调,javascript,jquery,twitter-bootstrap,jquery-events,Javascript,Jquery,Twitter Bootstrap,Jquery Events,我正在编写一个Javascript应用程序,它通过AJAX获取HTML文档,然后需要对其进行处理以将事件侦听器(特别是引导弹出窗口)附加到其中的元素。我很难连接听众,我认为这是一个范围问题。以下是相关代码: var App = function(site){ this.load = function(data, func){ $('div.ajax').html(data); func(); } this.dispatch = function(data){

我正在编写一个Javascript应用程序,它通过AJAX获取HTML文档,然后需要对其进行处理以将事件侦听器(特别是引导弹出窗口)附加到其中的元素。我很难连接听众,我认为这是一个范围问题。以下是相关代码:

var App = function(site){

  this.load = function(data, func){
    $('div.ajax').html(data);
    func();
  }

  this.dispatch = function(data){

    if(data.indexOf('from_server') !== -1){
      this.load(data, this.process);
      console.log('Loaded view from Matisse.');
      return true;
    }

  }

  this.process = function(){
    this.popovers('from_server');
  }

  this.popovers = function(el){
    var that = this;
    $('img.artwork.gallery', el).each(function(){
       $(this).popover({ trigger: 'hover', content: that.popoverPopulate(this) });
    });
  }

  this.popoverPopulate = function(el){
    return $(el).next('div.popover_content').html();
  }
}

var APP = new App();

$.ajax({blah: blah, success: function(data){ APP.dispatch(data); }});

...
问题(我认为)在于
func()
调用
this.load
。如果我传递它
this.process()
,那么它会将“this”的范围限定到窗口,并且会出现错误。如果我通过了
这个.process
,它就是一个创建的lambda,但仍然失败。如果调用
this.func()
也会出现同样的问题


我怎样才能a)通过回调将作用域保持在App对象内,或者b)重新组织此混乱以在加载后调用处理程序?

我想象您想要在所有方法上使用
var=this
作用域技巧:

var App = function(site){

  var that = this;

  this.load = function(data, func){
    $('div.ajax').html(data);
    func();
  }

  this.dispatch = function(data){

    if(data.indexOf('from_server') !== -1){
      that.load(data, that.process);
      console.log('Loaded view from Matisse.');
      return true;
    }

  }

  this.process = function(){
    that.popovers('from_server');
  }

  this.popovers = function(el){
    $('img.artwork.gallery', el).each(function(){
       $(that).popover({ trigger: 'hover', content: that.popoverPopulate(this) });
    });
  }

  this.popoverPopulate = function(el){
    return $(el).next('div.popover_content').html();
  }
}

我可以想象你想在所有方法上使用
var=this
scoping技巧:

var App = function(site){

  var that = this;

  this.load = function(data, func){
    $('div.ajax').html(data);
    func();
  }

  this.dispatch = function(data){

    if(data.indexOf('from_server') !== -1){
      that.load(data, that.process);
      console.log('Loaded view from Matisse.');
      return true;
    }

  }

  this.process = function(){
    that.popovers('from_server');
  }

  this.popovers = function(el){
    $('img.artwork.gallery', el).each(function(){
       $(that).popover({ trigger: 'hover', content: that.popoverPopulate(this) });
    });
  }

  this.popoverPopulate = function(el){
    return $(el).next('div.popover_content').html();
  }
}

这是指当前使用它的上下文。因此,当您执行
this.process时,它将以窗口为目标。如果执行
App.load(data,App.process)
操作,则它将以
App
对象中的流程函数为目标。

这是指当前使用它的上下文。因此,当您执行
this.process时,它将以窗口为目标。如果执行
App.load(数据,App.process)
操作,则它将以
App
对象中的流程函数为目标。

类似以下内容:

var App = function(site){
    var self = this; //-<!!!

    this.load = function(data, func){

    ...

    this.dispatch = function(data){
        if(data.indexOf('from_server') !== -1){
            self.load(data, self.process);
    ...
var-App=功能(站点){
var self=this;/-类似这样的内容:

var App = function(site){
    var self = this; //-<!!!

    this.load = function(data, func){

    ...

    this.dispatch = function(data){
        if(data.indexOf('from_server') !== -1){
            self.load(data, self.process);
    ...
var-App=功能(站点){

var self=this;//-hahahaha,我真的很喜欢你的“注意这个”评论!太棒了。还有,在最后一行(在“…”之前)你可能想在方法调用和传递的引用上使用
self
而不是
this
,不是吗?或者
this
仍然是对象,而不是
window
?哈哈哈,我真的很喜欢你的“注意这个”评论!太棒了。还有,在最后一行(在"...")你可能想在方法调用和你传递的引用上使用
self
而不是
this
,不?或者
this
仍然是对象,而不是
window
,因为它是第一个被接受的,但也要感谢伊利亚。我在得到答案时已经这样做了,但是谢谢。听众们仍然没有联系,但我认为这是Bootstrap的问题,而不是我的Javascript。啊,是的-我没有使用Bootstrap,所以我对它的问题不熟悉。祝你好运!(另外,我会支持@Ilia的荣誉-两分钟不算什么。)被接受是因为它是第一个出现的,但是伊利亚也很荣幸。在答案出现时我已经这样做了,但是谢谢你。听众仍然没有联系,但我认为这是Bootstrap的问题,不是我的Javascript。啊,是的,我没有使用Bootstrap,所以我不熟悉它的问题。祝你好运!(另外,我会在《伊利亚》中为大家鼓掌——两分钟不算什么。)