| Index: pkg/polymer/lib/elements/polymer-jsonp/polymer-jsonp.html
|
| diff --git a/pkg/polymer/lib/elements/polymer-jsonp/polymer-jsonp.html b/pkg/polymer/lib/elements/polymer-jsonp/polymer-jsonp.html
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..eadc1daf09249b9718d2e08903061c6cba1150cd
|
| --- /dev/null
|
| +++ b/pkg/polymer/lib/elements/polymer-jsonp/polymer-jsonp.html
|
| @@ -0,0 +1,95 @@
|
| +<!--
|
| +Copyright 2013 The Polymer Authors. All rights reserved.
|
| +Use of this source code is governed by a BSD-style
|
| +license that can be found in the LICENSE file.
|
| +-->
|
| +<!--
|
| +/**
|
| + * @module Polymer Elements
|
| + */
|
| +/**
|
| + * polymer-jsonp can be used to perform JSONP requests.
|
| +
|
| + * Example:
|
| + *
|
| + * <polymer-jsonp url="https://clients1.google.com/finance/info?q=GOOG&client=ig&callback="
|
| + * response="{{data}}"></polymer-jsonp>
|
| + *
|
| + * @class polymer-jsonp
|
| + */
|
| +/**
|
| + * Fired when a response is received.
|
| + *
|
| + * @event polymer-response
|
| + */
|
| +-->
|
| +<link rel="import" href="../polymer/polymer.html">
|
| +
|
| +<polymer-element name="polymer-jsonp" attributes="url response auto bustCache">
|
| + <script>
|
| + (function() {
|
| + Polymer('polymer-jsonp', {
|
| + /**
|
| + * The URL target of the request.
|
| + *
|
| + * @attribute url
|
| + * @type string
|
| + * @default ''
|
| + */
|
| + url: '',
|
| + /**
|
| + * Returns the response object.
|
| + *
|
| + * @attribute response
|
| + * @type Object
|
| + * @default null
|
| + */
|
| + response: null,
|
| + bustCache: false,
|
| + auto: false,
|
| + urlChanged: function() {
|
| + if (this.url && this.auto) {
|
| + this.go();
|
| + }
|
| + },
|
| + /**
|
| + * Performs a JSONP request to the url specified.
|
| + *
|
| + * @method go
|
| + */
|
| + go: function() {
|
| + if (!this.isInFlight()) {
|
| + this.callbackFunc = JSONP_CALLBACK_FUNC_NAME + callbackId++;
|
| + window[this.callbackFunc] = this.respond.bind(this);
|
| + var url = this.url + this.callbackFunc + (this.bustCache ? '&' + Math.random() : '');
|
| + this.addScript(url);
|
| + }
|
| + },
|
| + isInFlight: function() {
|
| + return !!this.script;
|
| + },
|
| + 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];
|
| + this.fire('polymer-response', {response: inResponse});
|
| + }
|
| + });
|
| + var JSONP_CALLBACK_FUNC_NAME = '_polymer_jsonp_callback_';
|
| + var callbackId = 0;
|
| + })();
|
| + </script>
|
| +</polymer-element>
|
|
|