4 Polymer('core-shared-lib',{
6 notifyEvent
: 'core-shared-lib-load',
9 if (!this.url
&& this.defaultUrl
) {
10 this.url
= this.defaultUrl
;
14 urlChanged: function() {
15 require(this.url
, this, this.callbackName
);
23 this.fire(this.notifyEvent
, arguments
);
30 function require(url
, notifiee
, callbackName
) {
31 // make hashable string form url
32 var name
= nameFromUrl(url
);
33 // lookup existing loader instance
34 var loader
= apiMap
[name
];
35 // create a loader as needed
37 loader
= apiMap
[name
] = new Loader(name
, url
, callbackName
);
39 loader
.requestNotify(notifiee
);
42 function nameFromUrl(url
) {
43 return url
.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api';
46 var Loader = function(name
, url
, callbackName
) {
48 this.callbackName
= callbackName
;
49 if (this.callbackName
) {
50 window
[this.callbackName
] = this.success
.bind(this);
52 if (url
.indexOf(this.callbackMacro
) >= 0) {
53 this.callbackName
= name
+ '_loaded';
54 window
[this.callbackName
] = this.success
.bind(this);
55 url
= url
.replace(this.callbackMacro
, this.callbackName
);
57 // TODO(sjmiles): we should probably fallback to listening to script.load
58 throw 'core-shared-api: a %%callback%% parameter is required in the API url';
67 callbackMacro
: '%%callback%%',
70 addScript: function(src
) {
71 var script
= document
.createElement('script');
73 script
.onerror
= this.error
.bind(this);
74 var s
= document
.querySelector('script');
75 s
.parentNode
.insertBefore(script
, s
);
79 removeScript: function() {
80 if (this.script
.parentNode
) {
81 this.script
.parentNode
.removeChild(this.script
);
93 this.result
= Array
.prototype.slice
.call(arguments
);
94 this.instances
.forEach(this.provide
, this);
95 this.instances
= null;
99 delete window
[this.callbackName
];
102 provide: function(instance
) {
103 instance
.notify(instance
, this.result
);
106 requestNotify: function(instance
) {
108 this.provide(instance
);
110 this.instances
.push(instance
);