剖析 mootools

发布于 2024-11-26 17:13:36 字数 1076 浏览 5 评论 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;
    }

,在这个clear函数中我可以看到destroy(),它将我带到shim(参见顶部的代码)并且我被卡住了。 所有这些功能都在 mootools-more.js

帮助?

如果有人可以提供 Request.JSONP.cancel 的纯 JavaScript 实现,那就太好了 它有 JQuery 替代品吗?

i had a problem and solving which required a functionality built in mootools. so i began dissecting it.
there a function a am interested in
IframeShim.destroy
its like this

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

now what i cant understand in this what is shim.
Paricularly what i am trying to understand is
Request.JSONP.cancel
its code is like this

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

now this cancel calls clear whose code is like this

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

now in this clear function i can see destroy() which takes me to shim(see code at the top) and i m stuck.
All these functions are in mootools-more.js

Help?

it would be great if somebody could provide a plain javascript implementation of Request.JSONP.cancel
Does it have a JQuery alternative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榆西 2024-12-03 17:13:36

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 所做的就是调用本机 jasvascript DOM 方法 Node.removeChild 从 DOM 中删除一个元素。

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

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

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

The destroy that is being called by the Request.JSONP.clear method isn't IframeShim.destory, it's part of the mootools core. Here's the source:

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

All that Element.dispose does is call the native jasvascript DOM method Node.removeChild which deletes an element from the DOM.

So all JSONP.cancel is doing is seeing if a script DOM node was added via Request.JSONP.cancel. If it was, it removes the script element from from the DOM via removeChild.

The important thing is that it set the running flag to false. If you look at Request.JSONP.success, the first thing it does before calling your callback function is check if the running flag is set to false, and if it is, it returns immediately. This effectively "cancels" the execution.

If you meant does it cancel the HTTP request, the answer is no, it does not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文