0

我遇到了一个需要内置功能的问题和解决方案mootools。所以我开始解剖它。有一个我对它感兴趣的 IframeShim.destroy 功能

destroy: function(){
        if (this.shim) this.shim.destroy();
        return this;
    }

现在我无法理解这是什么shim。特别是我想了解的是 Request.JSONP.cancel 它的代码是这样的

cancel: function(){
        if (this.running) this.clear().fireEvent('cancel');
        return this;
    }

现在这个取消调用clear其代码是这样的

clear: function(){
        this.running = false;
        if (this.script){
            this.script.destroy();
            this.script = null;
        }
        return this;
    }

现在在这个清晰的功能中,我可以看到destroy()哪个带我去shim(见顶部的代码)并且我卡住了。所有这些功能都在mootools-more.js

帮助?

如果有人可以提供一个普通的 JavaScript 实现,那就太好了。Request.JSONP.cancel 它有JQuery替代方案吗?

4

1 回答 1

1

Request.JSONP.clear方法调用的破坏不是IframeShim.destory,它是 mootools 核心的一部分。这是来源:

destroy: function(){
    var children = clean(this).getElementsByTagName('*');
    Array.each(children, clean);
    Element.dispose(this);
    return null;
},

所做Element.dispose的只是调用本地 jasvascript DOM 方法Node.removeChild从 DOM 中删除一个元素。

所以 JSONP.cancel 所做的就是查看脚本 DOM 节点是否通过Request.JSONP.cancel. 如果是,它会通过 removeChild 从 DOM 中删除脚本元素。

重要的是它将running标志设置为假。如果你看一下Request.JSONP.success,它在调用你的回调函数之前做的第一件事就是检查running标志是否设置为 false,如果是,它会立即返回。这有效地“取消”了执行。

如果您的意思是它是否取消 HTTP 请求,答案是否定的,它没有。

于 2011-07-29T09:57:57.520 回答