Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / ScriptTagProxy.html
diff --git a/docs/source/ScriptTagProxy.html b/docs/source/ScriptTagProxy.html
new file mode 100644 (file)
index 0000000..8b3b4f1
--- /dev/null
@@ -0,0 +1,282 @@
+<html>\r
+<head>\r
+  <title>The source code</title>\r
+    <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
+    <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
+</head>\r
+<body  onload="prettyPrint();">\r
+    <pre class="prettyprint lang-js"><div id="cls-Ext.data.ScriptTagProxy"></div>/**\r
+ * @class Ext.data.ScriptTagProxy\r
+ * @extends Ext.data.DataProxy\r
+ * An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain\r
+ * other than the originating domain of the running page.<br>\r
+ * <p>\r
+ * <b>Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain\r
+ * of the running page, you must use this class, rather than HttpProxy.</b><br>\r
+ * <p>\r
+ * The content passed back from a server resource requested by a ScriptTagProxy <b>must</b> be executable JavaScript\r
+ * source code because it is used as the source inside a &lt;script> tag.<br>\r
+ * <p>\r
+ * In order for the browser to process the returned data, the server must wrap the data object\r
+ * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.\r
+ * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy\r
+ * depending on whether the callback name was passed:\r
+ * <p>\r
+ * <pre><code>\r
+boolean scriptTag = false;\r
+String cb = request.getParameter("callback");\r
+if (cb != null) {\r
+    scriptTag = true;\r
+    response.setContentType("text/javascript");\r
+} else {\r
+    response.setContentType("application/x-json");\r
+}\r
+Writer out = response.getWriter();\r
+if (scriptTag) {\r
+    out.write(cb + "(");\r
+}\r
+out.print(dataBlock.toJsonString());\r
+if (scriptTag) {\r
+    out.write(");");\r
+}\r
+</code></pre>\r
+ *\r
+ * @constructor\r
+ * @param {Object} config A configuration object.\r
+ */\r
+Ext.data.ScriptTagProxy = function(config){\r
+    Ext.apply(this, config);\r
+\r
+    Ext.data.ScriptTagProxy.superclass.constructor.call(this, config);\r
+\r
+    this.head = document.getElementsByTagName("head")[0];\r
+\r
+    <div id="event-Ext.data.ScriptTagProxy-loadexception"></div>/**\r
+     * @event loadexception\r
+     * <b>Deprecated</b> in favor of 'exception' event.\r
+     * Fires if an exception occurs in the Proxy during data loading.  This event can be fired for one of two reasons:\r
+     * <ul><li><b>The load call timed out.</b>  This means the load callback did not execute within the time limit\r
+     * specified by {@link #timeout}.  In this case, this event will be raised and the\r
+     * fourth parameter (read error) will be null.</li>\r
+     * <li><b>The load succeeded but the reader could not read the response.</b>  This means the server returned\r
+     * data, but the configured Reader threw an error while reading the data.  In this case, this event will be\r
+     * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>\r
+     * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly\r
+     * on any Store instance.\r
+     * @param {Object} this\r
+     * @param {Object} options The loading options that were specified (see {@link #load} for details).  If the load\r
+     * call timed out, this parameter will be null.\r
+     * @param {Object} arg The callback's arg object passed to the {@link #load} function\r
+     * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.\r
+     * If the remote request returns success: false, this parameter will be null.\r
+     */\r
+};\r
+\r
+Ext.data.ScriptTagProxy.TRANS_ID = 1000;\r
+\r
+Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {\r
+    <div id="cfg-Ext.data.ScriptTagProxy-url"></div>/**\r
+     * @cfg {String} url The URL from which to request the data object.\r
+     */\r
+    <div id="cfg-Ext.data.ScriptTagProxy-timeout"></div>/**\r
+     * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.\r
+     */\r
+    timeout : 30000,\r
+    <div id="cfg-Ext.data.ScriptTagProxy-callbackParam"></div>/**\r
+     * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells\r
+     * the server the name of the callback function set up by the load call to process the returned data object.\r
+     * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate\r
+     * javascript output which calls this named function passing the data object as its only parameter.\r
+     */\r
+    callbackParam : "callback",\r
+    <div id="cfg-Ext.data.ScriptTagProxy-nocache"></div>/**\r
+     *  @cfg {Boolean} nocache (optional) Defaults to true. Disable caching by adding a unique parameter\r
+     * name to the request.\r
+     */\r
+    nocache : true,\r
+\r
+    <div id="method-Ext.data.ScriptTagProxy-doRequest"></div>/**\r
+     * HttpProxy implementation of DataProxy#doRequest\r
+     * @param {String} action\r
+     * @param {Ext.data.Record/Ext.data.Record[]} rs If action is <tt>read</tt>, rs will be null\r
+     * @param {Object} params An object containing properties which are to be used as HTTP parameters\r
+     * for the request to the remote server.\r
+     * @param {Ext.data.DataReader} reader The Reader object which converts the data\r
+     * object into a block of Ext.data.Records.\r
+     * @param {Function} callback The function into which to pass the block of Ext.data.Records.\r
+     * The function must be passed <ul>\r
+     * <li>The Record block object</li>\r
+     * <li>The "arg" argument from the load function</li>\r
+     * <li>A boolean success indicator</li>\r
+     * </ul>\r
+     * @param {Object} scope The scope in which to call the callback\r
+     * @param {Object} arg An optional argument which is passed to the callback as its second parameter.\r
+     */\r
+    doRequest : function(action, rs, params, reader, callback, scope, arg) {\r
+        var p = Ext.urlEncode(Ext.apply(params, this.extraParams));\r
+\r
+        var url = this.buildUrl(action, rs);\r
+        if (!url) {\r
+            throw new Ext.data.Api.Error('invalid-url', url);\r
+        }\r
+        url = Ext.urlAppend(url, p);\r
+\r
+        if(this.nocache){\r
+            url = Ext.urlAppend(url, '_dc=' + (new Date().getTime()));\r
+        }\r
+        var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;\r
+        var trans = {\r
+            id : transId,\r
+            action: action,\r
+            cb : "stcCallback"+transId,\r
+            scriptId : "stcScript"+transId,\r
+            params : params,\r
+            arg : arg,\r
+            url : url,\r
+            callback : callback,\r
+            scope : scope,\r
+            reader : reader\r
+        };\r
+        window[trans.cb] = this.createCallback(action, rs, trans);\r
+        url += String.format("&{0}={1}", this.callbackParam, trans.cb);\r
+        if(this.autoAbort !== false){\r
+            this.abort();\r
+        }\r
+\r
+        trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);\r
+\r
+        var script = document.createElement("script");\r
+        script.setAttribute("src", url);\r
+        script.setAttribute("type", "text/javascript");\r
+        script.setAttribute("id", trans.scriptId);\r
+        this.head.appendChild(script);\r
+\r
+        this.trans = trans;\r
+    },\r
+\r
+    // @private createCallback\r
+    createCallback : function(action, rs, trans) {\r
+        var self = this;\r
+        return function(res) {\r
+            self.trans = false;\r
+            self.destroyTrans(trans, true);\r
+            if (action === Ext.data.Api.actions.read) {\r
+                self.onRead.call(self, action, trans, res);\r
+            } else {\r
+                self.onWrite.call(self, action, trans, res, rs);\r
+            }\r
+        };\r
+    },\r
+    /**\r
+     * Callback for read actions\r
+     * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
+     * @param {Object} trans The request transaction object\r
+     * @param {Object} res The server response\r
+     * @private\r
+     */\r
+    onRead : function(action, trans, res) {\r
+        var result;\r
+        try {\r
+            result = trans.reader.readRecords(res);\r
+        }catch(e){\r
+            // @deprecated: fire loadexception\r
+            this.fireEvent("loadexception", this, trans, res, e);\r
+\r
+            this.fireEvent('exception', this, 'response', action, trans, res, e);\r
+            trans.callback.call(trans.scope||window, null, trans.arg, false);\r
+            return;\r
+        }\r
+        if (result.success === false) {\r
+            // @deprecated: fire old loadexception for backwards-compat.\r
+            this.fireEvent('loadexception', this, trans, res);\r
+\r
+            this.fireEvent('exception', this, 'remote', action, trans, res, null);\r
+        } else {\r
+            this.fireEvent("load", this, res, trans.arg);\r
+        }\r
+        trans.callback.call(trans.scope||window, result, trans.arg, result.success);\r
+    },\r
+    /**\r
+     * Callback for write actions\r
+     * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
+     * @param {Object} trans The request transaction object\r
+     * @param {Object} res The server response\r
+     * @private\r
+     */\r
+    onWrite : function(action, trans, res, rs) {\r
+        var reader = trans.reader;\r
+        try {\r
+            // though we already have a response object here in STP, run through readResponse to catch any meta-data exceptions.\r
+            reader.readResponse(action, res);\r
+        } catch (e) {\r
+            this.fireEvent('exception', this, 'response', action, trans, res, e);\r
+            trans.callback.call(trans.scope||window, null, res, false);\r
+            return;\r
+        }\r
+        if(!res[reader.meta.successProperty] === true){\r
+            this.fireEvent('exception', this, 'remote', action, trans, res, rs);\r
+            trans.callback.call(trans.scope||window, null, res, false);\r
+            return;\r
+        }\r
+        this.fireEvent("write", this, action, res[reader.meta.root], res, rs, trans.arg );\r
+        trans.callback.call(trans.scope||window, res[reader.meta.root], res, true);\r
+    },\r
+\r
+    // private\r
+    isLoading : function(){\r
+        return this.trans ? true : false;\r
+    },\r
+\r
+    <div id="method-Ext.data.ScriptTagProxy-abort"></div>/**\r
+     * Abort the current server request.\r
+     */\r
+    abort : function(){\r
+        if(this.isLoading()){\r
+            this.destroyTrans(this.trans);\r
+        }\r
+    },\r
+\r
+    // private\r
+    destroyTrans : function(trans, isLoaded){\r
+        this.head.removeChild(document.getElementById(trans.scriptId));\r
+        clearTimeout(trans.timeoutId);\r
+        if(isLoaded){\r
+            window[trans.cb] = undefined;\r
+            try{\r
+                delete window[trans.cb];\r
+            }catch(e){}\r
+        }else{\r
+            // if hasn't been loaded, wait for load to remove it to prevent script error\r
+            window[trans.cb] = function(){\r
+                window[trans.cb] = undefined;\r
+                try{\r
+                    delete window[trans.cb];\r
+                }catch(e){}\r
+            };\r
+        }\r
+    },\r
+\r
+    // private\r
+    handleFailure : function(trans){\r
+        this.trans = false;\r
+        this.destroyTrans(trans, false);\r
+        if (trans.action === Ext.data.Api.actions.read) {\r
+            // @deprecated firing loadexception\r
+            this.fireEvent("loadexception", this, null, trans.arg);\r
+        }\r
+\r
+        this.fireEvent('exception', this, 'response', trans.action, {\r
+            response: null,\r
+            options: trans.arg\r
+        });\r
+        trans.callback.call(trans.scope||window, null, trans.arg, false);\r
+    },\r
+\r
+    // inherit docs\r
+    destroy: function(){\r
+        this.abort();\r
+        Ext.data.ScriptTagProxy.superclass.destroy.call(this);\r
+    }\r
+});</pre>    \r
+</body>\r
+</html>
\ No newline at end of file