dissecting mootools

dissecting mootools

本文关键字:mootools dissecting      更新时间:2023-09-26

我遇到了一个问题,需要在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;
    }

现在这个cancel调用clear,其代码如下

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

现在在这个清晰的函数中,我可以看到destroy(),它把我带到shim(见顶部的代码),我卡住了。所有这些函数都在mootools-more.js

帮助吗?

如果有人能提供 Request.JSONP.cancel 的简单javascript实现就太好了它有JQuery替代品吗?

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

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

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

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

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

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