Skip to content

Commit

Permalink
jsonp component
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiefu committed Jan 23, 2013
1 parent dbb598d commit 5963fef
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/g-jsonp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
Copyright 2012 The Toolkitchen Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<element name="g-jsonp" attributes="url response">
<link rel="components" href="g-component.html">
<script>
var JSONP_CALLBACK_FUNC_NAME = 'jsonp_callback_';

var callbackId = 0;

this.component({
publish: {
go: function() {
this.callbackFunc = JSONP_CALLBACK_FUNC_NAME + callbackId++;
window[this.callbackFunc] = this.respond.bind(this);
url = this.url + this.callbackFunc + '&' + Math.random();
this.addScript(url);
}
},
ready: function() {
if (this.url) {
this.go();
}
},
addScript: function(inSrc) {
this.script = document.createElement('script');
this.script.src = inSrc;
this.script.onerror = this.respond.bind(this);
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(this.script, s);
},
removeScript: function() {
if (this.script.parentNode) {
this.script.parentNode.removeChild(this.script);
}
this.script = null;
},
respond: function(inResponse) {
this.response = inResponse;
this.removeScript();
delete window[this.callbackFunc];
}
});
</script>
</element>

0 comments on commit 5963fef

Please sign in to comment.