BVB Source Codes

PreloadJS Show JSONPLoader.js Source code

Return Download PreloadJS: download JSONPLoader.js Source code - Download PreloadJS Source code - Type:.js
  1. /*
  2.  * JSONPLoader
  3.  * Visit http://createjs.com/ for documentation, updates and examples.
  4.  *
  5.  *
  6.  * Copyright (c) 2012 gskinner.com, inc.
  7.  *
  8.  * Permission is hereby granted, free of charge, to any person
  9.  * obtaining a copy of this software and associated documentation
  10.  * files (the "Software"), to deal in the Software without
  11.  * restriction, including without limitation the rights to use,
  12.  * copy, modify, merge, publish, distribute, sublicense, and/or sell
  13.  * copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following
  15.  * conditions:
  16.  *
  17.  * The above copyright notice and this permission notice shall be
  18.  * included in all copies or substantial portions of the Software.
  19.  *
  20.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24.  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25.  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27.  * OTHER DEALINGS IN THE SOFTWARE.
  28.  */
  29.  
  30. /**
  31.  * @module PreloadJS
  32.  */
  33.  
  34. // namespace:
  35. this.createjs = this.createjs || {};
  36.  
  37. (function () {
  38.         "use strict";
  39.  
  40.         // constructor
  41.         /**
  42.          * A loader for JSONP files, which are JSON-formatted text files, wrapped in a callback. To load regular JSON
  43.          * without a callback use the {{#crossLink "JSONLoader"}}{{/crossLink}} instead. To load JSON-formatted manifests,
  44.          * use {{#crossLink "ManifestLoader"}}{{/crossLink}}, and to load EaselJS SpriteSheets, use
  45.          * {{#crossLink "SpriteSheetLoader"}}{{/crossLink}}.
  46.          *
  47.          * JSONP is a format that provides a solution for loading JSON files cross-domain <em>without</em> requiring CORS.
  48.          * JSONP files are loaded as JavaScript, and the "callback" is executed once they are loaded. The callback in the
  49.          * JSONP must match the callback passed to the loadItem.
  50.          *
  51.          * <h4>Example JSONP</h4>
  52.          *
  53.          *              callbackName({
  54.          *                      "name": "value",
  55.          *                      "num": 3,
  56.          *                      "obj": { "bool":true }
  57.          *              });
  58.          *
  59.          * <h4>Example</h4>
  60.          *
  61.          *              var loadItem = {id:"json", type:"jsonp", src:"http://server.com/text.json", callback:"callbackName"}
  62.          *              var queue = new createjs.LoadQueue();
  63.          *              queue.on("complete", handleComplete);
  64.          *              queue.loadItem(loadItem);
  65.          *
  66.          *              function handleComplete(event) }
  67.          *                      var json = queue.getResult("json");
  68.          *                      console.log(json.obj.bool); // true
  69.          *              }
  70.          *
  71.          * Note that JSONP files loaded concurrently require a <em>unique</em> callback. To ensure JSONP files are loaded
  72.          * in order, either use the {{#crossLink "LoadQueue/setMaxConnections"}}{{/crossLink}} method (set to 1),
  73.          * or set {{#crossLink "LoadItem/maintainOrder:property"}}{{/crossLink}} on items with the same callback.
  74.          *
  75.          * @class JSONPLoader
  76.          * @param {LoadItem|Object} loadItem
  77.          * @extends AbstractLoader
  78.          * @constructor
  79.          */
  80.         function JSONPLoader(loadItem) {
  81.                 this.AbstractLoader_constructor(loadItem, false, createjs.AbstractLoader.JSONP);
  82.                 this.setTag(createjs.Elements.script());
  83.                 this.getTag().type = "text/javascript";
  84.         };
  85.  
  86.         var p = createjs.extend(JSONPLoader, createjs.AbstractLoader);
  87.         var s = JSONPLoader;
  88.  
  89.  
  90.         // static methods
  91.         /**
  92.          * Determines if the loader can load a specific item. This loader can only load items that are of type
  93.          * {{#crossLink "AbstractLoader/JSONP:property"}}{{/crossLink}}.
  94.          * @method canLoadItem
  95.          * @param {LoadItem|Object} item The LoadItem that a LoadQueue is trying to load.
  96.          * @returns {Boolean} Whether the loader can load the item.
  97.          * @static
  98.          */
  99.         s.canLoadItem = function (item) {
  100.                 return item.type == createjs.AbstractLoader.JSONP;
  101.         };
  102.  
  103.         // public methods
  104.         p.cancel = function () {
  105.                 this.AbstractLoader_cancel();
  106.                 this._dispose();
  107.         };
  108.  
  109.         /**
  110.          * Loads the JSONp file.  Because of the unique loading needs of JSONp
  111.          * we don't use the AbstractLoader.load() method.
  112.          *
  113.          * @method load
  114.          *
  115.          */
  116.         p.load = function () {
  117.                 if (this._item.callback == null) {
  118.                         throw new Error('callback is required for loading JSONP requests.');
  119.                 }
  120.  
  121.                 // TODO: Look into creating our own iFrame to handle the load
  122.                 // In the first attempt, FF did not get the result
  123.                 //   result instanceof Object did not work either
  124.                 //   so we would need to clone the result.
  125.                 if (window[this._item.callback] != null) {
  126.                         throw new Error(
  127.                                 "JSONP callback '" +
  128.                                 this._item.callback +
  129.                                 "' already exists on window. You need to specify a different callback or re-name the current one.");
  130.                 }
  131.  
  132.                 window[this._item.callback] = createjs.proxy(this._handleLoad, this);
  133.                 createjs.DomUtils.appendToBody(this._tag);
  134.  
  135.                 this._loadTimeout = setTimeout(createjs.proxy(this._handleTimeout, this), this._item.loadTimeout);
  136.  
  137.                 // Load the tag
  138.                 this._tag.src = this._item.src;
  139.         };
  140.  
  141.         // private methods
  142.         /**
  143.          * Handle the JSONP callback, which is a public method defined on `window`.
  144.          * @method _handleLoad
  145.          * @param {Object} data The formatted JSON data.
  146.          * @private
  147.          */
  148.         p._handleLoad = function (data) {
  149.                 this._result = this._rawResult = data;
  150.                 this._sendComplete();
  151.  
  152.                 this._dispose();
  153.         };
  154.  
  155.         /**
  156.          * The tag request has not loaded within the time specfied in loadTimeout.
  157.          * @method _handleError
  158.          * @param {Object} event The XHR error event.
  159.          * @private
  160.          */
  161.         p._handleTimeout = function () {
  162.                 this._dispose();
  163.                 this.dispatchEvent(new createjs.ErrorEvent("timeout"));
  164.         };
  165.  
  166.         /**
  167.          * Clean up the JSONP load. This clears out the callback and script tag that this loader creates.
  168.          * @method _dispose
  169.          * @private
  170.          */
  171.         p._dispose = function () {
  172.                 createjs.DomUtils.removeChild(this._tag);
  173.                 delete window[this._item.callback];
  174.  
  175.                 clearTimeout(this._loadTimeout);
  176.         };
  177.  
  178.         createjs.JSONPLoader = createjs.promote(JSONPLoader, "AbstractLoader");
  179.  
  180. }());
  181.  
downloadJSONPLoader.js Source code - Download PreloadJS Source code
Related Source Codes/Software:
gotraining - Go Training Class Material : ... 2017-04-29
eigen - The Art World in Your Pocket or Your Trendy Tech C... 2017-04-29
rack-cors - Rack Middleware for handling Cross-Origin Resource... 2017-04-29
react-redux-tutorial - react-redux-tutorial in Chinese, catalog &&... 2017-04-29
swifter - Tiny http server engine written in Swift programmi... 2017-04-29
boto3 - AWS SDK for Python http://a... 2017-04-30
Fluidbox - Replicating and improving the lightbox module seen... 2017-04-30
jo - JSON output from a shell 2017-04-30
qTip2 - qTip2 - Pretty powerful tooltips ... 2017-04-30
swing - A swipeable cards interface. The swipe-left/swipe-... 2017-04-30
growth - Growth - App to help you Be Awesome Developer &... 2017-05-06
TourGuide - TourGuide is an Android library that aims to provi... 2017-05-06
urh - Universal Radio Hacker: investigate wireless proto... 2017-05-06
Gutenberg - A meaningful web typography starter kit. 2017-05-06
NiftyDialogEffects - Nifty Modal Dialog Effect 2017-05-06
opengapps - The main repository of the Open GApps Project ... 2017-05-06
The micro CMS network asked administration system (the original PHP168 S series) v20161230 - 1, upgraded the mobile version. 2, upgrade the st... 2017-05-06
The news industry marketing website HTML 5 v5.7 site source code - Dedecms latest version of kernel development and o... 2017-05-06
Dream weaving white large entrepreneurial wealth information class site template v1.0 - Dream white large entrepreneurial wealth of inform... 2017-05-06

 Back to top