0

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?

lovesh
  • 5,235
  • 9
  • 62
  • 93

1 Answers1

1

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.

Philip Ramirez
  • 2,972
  • 1
  • 16
  • 16
  • thanks,+1. does calling `destroy()`on a node cancels the any http requests made by the node.if u dont understand what i mean please see [this](http://stackoverflow.com/questions/6865574/how-to-cancel-http-request-using-javascript) – lovesh Jul 29 '11 at 12:19
  • does that mean that `Request.JSONP.cancel` cannot cancel a HTTP request? – lovesh Jul 29 '11 at 12:27
  • @lovesh no it does not cancel the HTTP request. What it does is it cancels the callback from executing once the script is loaded. – Philip Ramirez Jul 29 '11 at 13:05
  • 1
    @lovesh notice in `Request.JSONP.send` they set the src to be the URL passed in the options + `callback=Request.JSONP.request_map.request_'+ index`. This links it to a mapped `Request.JSONP.success` which, before executing, first checks if this.running is false, and if it is, returns immediately and thus "cancels" it (since the assigned callback will never execute). – Philip Ramirez Jul 29 '11 at 13:07