| OLD | NEW |
| 1 | 1 |
| 2 (function() { | 2 (function() { |
| 3 "use strict"; | 3 "use strict"; |
| 4 /** | 4 /** |
| 5 `Polymer.IronJsonpLibraryBehavior` loads a jsonp library. | 5 `Polymer.IronJsonpLibraryBehavior` loads a jsonp library. |
| 6 Multiple components can request same library, only one copy will load. | 6 Multiple components can request same library, only one copy will load. |
| 7 | 7 |
| 8 Some libraries require a specific global function be defined. | 8 Some libraries require a specific global function be defined. |
| 9 If this is the case, specify the `callbackName` property. | 9 If this is the case, specify the `callbackName` property. |
| 10 | 10 |
| 11 You should use an HTML Import to load library dependencies | 11 You should use an HTML Import to load library dependencies |
| 12 when possible instead of using this element. | 12 when possible instead of using this element. |
| 13 | 13 |
| 14 @hero hero.svg | 14 @hero hero.svg |
| 15 @demo demo/index.html |
| 15 @polymerBehavior | 16 @polymerBehavior |
| 16 */ | 17 */ |
| 17 Polymer.IronJsonpLibraryBehavior = { | 18 Polymer.IronJsonpLibraryBehavior = { |
| 18 | 19 |
| 19 properties: { | 20 properties: { |
| 20 /** | 21 /** |
| 21 * True if library has been successfully loaded | 22 * True if library has been successfully loaded |
| 22 */ | 23 */ |
| 23 libraryLoaded: { | 24 libraryLoaded: { |
| 24 type: Boolean, | 25 type: Boolean, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 this._libraryLoadCallback.bind(this), | 81 this._libraryLoadCallback.bind(this), |
| 81 this.callbackName | 82 this.callbackName |
| 82 ); | 83 ); |
| 83 }, | 84 }, |
| 84 | 85 |
| 85 ready: function() { | 86 ready: function() { |
| 86 this._loadLibrary(); | 87 this._loadLibrary(); |
| 87 } | 88 } |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 /* | 91 /** |
| 91 * LoaderMap keeps track of all Loaders | 92 * LoaderMap keeps track of all Loaders |
| 92 */ | 93 */ |
| 93 var LoaderMap = { | 94 var LoaderMap = { |
| 94 apiMap: {}, // { hash -> Loader } | 95 apiMap: {}, // { hash -> Loader } |
| 95 | 96 |
| 96 /* | 97 /** |
| 97 * @param {function} notifyCallback loaded callback fn(result) | 98 * @param {Function} notifyCallback loaded callback fn(result) |
| 98 * @param {string} jsonpCallbackName name of jsonpcallback. If API does not
provide it, leave empty. Optional. | 99 * @param {string} jsonpCallbackName name of jsonpcallback. If API does not
provide it, leave empty. Optional. |
| 99 */ | 100 */ |
| 100 require: function(url, notifyCallback, jsonpCallbackName) { | 101 require: function(url, notifyCallback, jsonpCallbackName) { |
| 101 | 102 |
| 102 // make hashable string form url | 103 // make hashable string form url |
| 103 var name = this.nameFromUrl(url); | 104 var name = this.nameFromUrl(url); |
| 104 | 105 |
| 105 // create a loader as needed | 106 // create a loader as needed |
| 106 if (!this.apiMap[name]) | 107 if (!this.apiMap[name]) |
| 107 this.apiMap[name] = new Loader(name, url, jsonpCallbackName); | 108 this.apiMap[name] = new Loader(name, url, jsonpCallbackName); |
| 108 | 109 |
| 109 // ask for notification | 110 // ask for notification |
| 110 this.apiMap[name].requestNotify(notifyCallback); | 111 this.apiMap[name].requestNotify(notifyCallback); |
| 111 }, | 112 }, |
| 112 | 113 |
| 113 nameFromUrl: function(url) { | 114 nameFromUrl: function(url) { |
| 114 return url.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api'; | 115 return url.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api'; |
| 115 } | 116 } |
| 116 }; | 117 }; |
| 117 | 118 |
| 119 /** @constructor */ |
| 118 var Loader = function(name, url, callbackName) { | 120 var Loader = function(name, url, callbackName) { |
| 119 this.notifiers = []; // array of notifyFn [ notifyFn* ] | 121 this.notifiers = []; // array of notifyFn [ notifyFn* ] |
| 120 | 122 |
| 121 // callback is specified either as callback name | 123 // callback is specified either as callback name |
| 122 // or computed dynamically if url has callbackMacro in it | 124 // or computed dynamically if url has callbackMacro in it |
| 123 if (!callbackName) { | 125 if (!callbackName) { |
| 124 if (url.indexOf(this.callbackMacro) >= 0) { | 126 if (url.indexOf(this.callbackMacro) >= 0) { |
| 125 callbackName = name + '_loaded'; | 127 callbackName = name + '_loaded'; |
| 126 url = url.replace(this.callbackMacro, callbackName); | 128 url = url.replace(this.callbackMacro, callbackName); |
| 127 } else { | 129 } else { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 this.loaded = true; | 168 this.loaded = true; |
| 167 this.result = Array.prototype.slice.call(arguments); | 169 this.result = Array.prototype.slice.call(arguments); |
| 168 this.notifyAll(); | 170 this.notifyAll(); |
| 169 this.cleanup(); | 171 this.cleanup(); |
| 170 }, | 172 }, |
| 171 | 173 |
| 172 cleanup: function() { | 174 cleanup: function() { |
| 173 delete window[this.callbackName]; | 175 delete window[this.callbackName]; |
| 174 }, | 176 }, |
| 175 | 177 |
| 176 notifyAll: function(notifyCallback) { | 178 notifyAll: function() { |
| 177 this.notifiers.forEach( function(notifyCallback) { | 179 this.notifiers.forEach( function(notifyCallback) { |
| 178 notifyCallback(this.error, this.result); | 180 notifyCallback(this.error, this.result); |
| 179 }.bind(this)); | 181 }.bind(this)); |
| 180 this.notifiers = []; | 182 this.notifiers = []; |
| 181 }, | 183 }, |
| 182 | 184 |
| 183 requestNotify: function(notifyCallback) { | 185 requestNotify: function(notifyCallback) { |
| 184 if (this.loaded || this.error) { | 186 if (this.loaded || this.error) { |
| 185 notifyCallback( this.error, this.result); | 187 notifyCallback( this.error, this.result); |
| 186 } else { | 188 } else { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 notifyEvent: String | 220 notifyEvent: String |
| 219 /** | 221 /** |
| 220 * event with name specified in 'notifyEvent' attribute | 222 * event with name specified in 'notifyEvent' attribute |
| 221 * will fire upon successful load | 223 * will fire upon successful load |
| 222 * @event `notifyEvent` | 224 * @event `notifyEvent` |
| 223 */ | 225 */ |
| 224 | 226 |
| 225 } | 227 } |
| 226 }); | 228 }); |
| 227 | 229 |
| OLD | NEW |