| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 @license | 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also | 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> | 9 --> |
| 10 <link rel="import" href="../polymer/polymer.html"> | 10 <link rel="import" href="../polymer/polymer.html"> |
| 11 | 11 |
| 12 <script> | 12 <script> |
| 13 (function() { | 13 (function() { |
| 14 "use strict"; | 14 "use strict"; |
| 15 /** | 15 /** |
| 16 `Polymer.IronJsonpLibraryBehavior` loads a jsonp library. | 16 `Polymer.IronJsonpLibraryBehavior` loads a jsonp library. |
| 17 Multiple components can request same library, only one copy will load. | 17 Multiple components can request same library, only one copy will load. |
| 18 | 18 |
| 19 Some libraries require a specific global function be defined. | 19 Some libraries require a specific global function be defined. |
| 20 If this is the case, specify the `callbackName` property. | 20 If this is the case, specify the `callbackName` property. |
| 21 | 21 |
| 22 You should use an HTML Import to load library dependencies | 22 You should use an HTML Import to load library dependencies |
| 23 when possible instead of using this element. | 23 when possible instead of using this element. |
| 24 | 24 |
| 25 @hero hero.svg | 25 @hero hero.svg |
| 26 @demo demo/index.html |
| 26 @polymerBehavior | 27 @polymerBehavior |
| 27 */ | 28 */ |
| 28 Polymer.IronJsonpLibraryBehavior = { | 29 Polymer.IronJsonpLibraryBehavior = { |
| 29 | 30 |
| 30 properties: { | 31 properties: { |
| 31 /** | 32 /** |
| 32 * True if library has been successfully loaded | 33 * True if library has been successfully loaded |
| 33 */ | 34 */ |
| 34 libraryLoaded: { | 35 libraryLoaded: { |
| 35 type: Boolean, | 36 type: Boolean, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 this._libraryLoadCallback.bind(this), | 92 this._libraryLoadCallback.bind(this), |
| 92 this.callbackName | 93 this.callbackName |
| 93 ); | 94 ); |
| 94 }, | 95 }, |
| 95 | 96 |
| 96 ready: function() { | 97 ready: function() { |
| 97 this._loadLibrary(); | 98 this._loadLibrary(); |
| 98 } | 99 } |
| 99 }; | 100 }; |
| 100 | 101 |
| 101 /* | 102 /** |
| 102 * LoaderMap keeps track of all Loaders | 103 * LoaderMap keeps track of all Loaders |
| 103 */ | 104 */ |
| 104 var LoaderMap = { | 105 var LoaderMap = { |
| 105 apiMap: {}, // { hash -> Loader } | 106 apiMap: {}, // { hash -> Loader } |
| 106 | 107 |
| 107 /* | 108 /** |
| 108 * @param {function} notifyCallback loaded callback fn(result) | 109 * @param {Function} notifyCallback loaded callback fn(result) |
| 109 * @param {string} jsonpCallbackName name of jsonpcallback. If API does not
provide it, leave empty. Optional. | 110 * @param {string} jsonpCallbackName name of jsonpcallback. If API does not
provide it, leave empty. Optional. |
| 110 */ | 111 */ |
| 111 require: function(url, notifyCallback, jsonpCallbackName) { | 112 require: function(url, notifyCallback, jsonpCallbackName) { |
| 112 | 113 |
| 113 // make hashable string form url | 114 // make hashable string form url |
| 114 var name = this.nameFromUrl(url); | 115 var name = this.nameFromUrl(url); |
| 115 | 116 |
| 116 // create a loader as needed | 117 // create a loader as needed |
| 117 if (!this.apiMap[name]) | 118 if (!this.apiMap[name]) |
| 118 this.apiMap[name] = new Loader(name, url, jsonpCallbackName); | 119 this.apiMap[name] = new Loader(name, url, jsonpCallbackName); |
| 119 | 120 |
| 120 // ask for notification | 121 // ask for notification |
| 121 this.apiMap[name].requestNotify(notifyCallback); | 122 this.apiMap[name].requestNotify(notifyCallback); |
| 122 }, | 123 }, |
| 123 | 124 |
| 124 nameFromUrl: function(url) { | 125 nameFromUrl: function(url) { |
| 125 return url.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api'; | 126 return url.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api'; |
| 126 } | 127 } |
| 127 }; | 128 }; |
| 128 | 129 |
| 130 /** @constructor */ |
| 129 var Loader = function(name, url, callbackName) { | 131 var Loader = function(name, url, callbackName) { |
| 130 this.notifiers = []; // array of notifyFn [ notifyFn* ] | 132 this.notifiers = []; // array of notifyFn [ notifyFn* ] |
| 131 | 133 |
| 132 // callback is specified either as callback name | 134 // callback is specified either as callback name |
| 133 // or computed dynamically if url has callbackMacro in it | 135 // or computed dynamically if url has callbackMacro in it |
| 134 if (!callbackName) { | 136 if (!callbackName) { |
| 135 if (url.indexOf(this.callbackMacro) >= 0) { | 137 if (url.indexOf(this.callbackMacro) >= 0) { |
| 136 callbackName = name + '_loaded'; | 138 callbackName = name + '_loaded'; |
| 137 url = url.replace(this.callbackMacro, callbackName); | 139 url = url.replace(this.callbackMacro, callbackName); |
| 138 } else { | 140 } else { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 this.loaded = true; | 179 this.loaded = true; |
| 178 this.result = Array.prototype.slice.call(arguments); | 180 this.result = Array.prototype.slice.call(arguments); |
| 179 this.notifyAll(); | 181 this.notifyAll(); |
| 180 this.cleanup(); | 182 this.cleanup(); |
| 181 }, | 183 }, |
| 182 | 184 |
| 183 cleanup: function() { | 185 cleanup: function() { |
| 184 delete window[this.callbackName]; | 186 delete window[this.callbackName]; |
| 185 }, | 187 }, |
| 186 | 188 |
| 187 notifyAll: function(notifyCallback) { | 189 notifyAll: function() { |
| 188 this.notifiers.forEach( function(notifyCallback) { | 190 this.notifiers.forEach( function(notifyCallback) { |
| 189 notifyCallback(this.error, this.result); | 191 notifyCallback(this.error, this.result); |
| 190 }.bind(this)); | 192 }.bind(this)); |
| 191 this.notifiers = []; | 193 this.notifiers = []; |
| 192 }, | 194 }, |
| 193 | 195 |
| 194 requestNotify: function(notifyCallback) { | 196 requestNotify: function(notifyCallback) { |
| 195 if (this.loaded || this.error) { | 197 if (this.loaded || this.error) { |
| 196 notifyCallback( this.error, this.result); | 198 notifyCallback( this.error, this.result); |
| 197 } else { | 199 } else { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 /** | 250 /** |
| 249 * event with name specified in 'notifyEvent' attribute | 251 * event with name specified in 'notifyEvent' attribute |
| 250 * will fire upon successful load | 252 * will fire upon successful load |
| 251 * @event `notifyEvent` | 253 * @event `notifyEvent` |
| 252 */ | 254 */ |
| 253 | 255 |
| 254 } | 256 } |
| 255 }); | 257 }); |
| 256 | 258 |
| 257 </script> | 259 </script> |
| OLD | NEW |