简体   繁体   English

JavaScript代码在回调中无效

[英]JavaScript code not working within a callback

In the following JavaScript code, if warnBeforeNew is false, the file open code works. 在以下JavaScript代码中,如果warnBeforeNew为false,则文件打开代码有效。 However, it doesn't if warnBeforeNew is true, instead giving an error "Uncaught TypeError: Cannot read property 'root' of undefined". 但是,如果warnBeforeNew为true,则不会出现错误“Uncaught TypeError:无法读取未定义的属性'root'”。

I don't know if this is to do with scoping, but how do I get the file loading code to work within the callback? 我不知道这是否与范围有关,但如何让文件加载代码在回调中工作? Thanks. 谢谢。

Editor.prototype.open = function(path) {
  if (Editor.warnBeforeNew==true){
    this.showDialog({
        dialogLabel: 'You have unsaved changes. Are you sure you want to discard them and open a different file?',
        submitLabel: 'Discard',
        cancelLabel: 'Cancel',
        submitCallback: function() {
          Editor.warnBeforeNew=false;
          this.filesystem.root.getFile(path, {}, this.load.bind(this), error.bind(null, "getFile " + path));
        }
    });
  } else {
    this.filesystem.root.getFile(path, {}, this.load.bind(this), error.bind(null, "getFile " + path));
  }
};

You have to save the value of this because when the callback is called, it's with a different receiver than the external function : 你要保存的价值this是因为,当回调被调用时,它与不同的接收器比外部函数:

if (Editor.warnBeforeNew==true){
    var thing = this; // choose a more meaningful name if possible...
    this.showDialog({
        dialogLabel: 'You have unsaved changes. Are you sure you want to discard them and open a different file?',
        submitLabel: 'Discard',
        cancelLabel: 'Cancel',
        submitCallback: function() {
          Editor.warnBeforeNew=false;
          thing.filesystem.root.getFile(path, {}, thing.load.bind(thing), error.bind(null, "getFile " + path));
        }
    });
  } else {
    this.filesystem.root.getFile(path, {}, this.load.bind(this), error.bind(null, "getFile " + path));
  }

Try to catch the scope out side the callback and use it. 尝试在回调中捕获范围并使用它。

Editor.prototype.open = function(path) {
var that=this;
  if (Editor.warnBeforeNew==true){
    this.showDialog({
        dialogLabel: 'You have unsaved changes. Are you sure you want to discard them and open a different file?',
        submitLabel: 'Discard',
        cancelLabel: 'Cancel',
        submitCallback: function() {
          Editor.warnBeforeNew=false;
          that.filesystem.root.getFile(path, {}, that.load.bind(that), error.bind(null, "getFile " + path));
        }
    });
  } else {
    this.filesystem.root.getFile(path, {}, this.load.bind(this), error.bind(null, "getFile " + path));
  }
};

The submitCallback to your showDialog method needs to be bound, too - it accesses this.filesystem.root.… which fails. 您的showDialog方法的submitCallback也需要绑定 - 它访问this.filesystem.root.…失败。

this.showDialog({
    …,
    submitCallback: function() {
      Editor.warnBeforeNew=false;
      this.filesystem.root.getFile(path, {}, this.load.bind(this), error.bind(null, "getFile " + path));
    }.bind(this)
//    ^^^^^^^^^^
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM