| Index: appengine/swarming/elements/res/imp/common/common-behavior.html
|
| diff --git a/appengine/swarming/elements/res/imp/common/common-behavior.html b/appengine/swarming/elements/res/imp/common/common-behavior.html
|
| index 4aefc6b700acf9b6d5ac08ecd982003c36a968e6..25c61710200c3d5ec02eda7da52568b6532e597b 100644
|
| --- a/appengine/swarming/elements/res/imp/common/common-behavior.html
|
| +++ b/appengine/swarming/elements/res/imp/common/common-behavior.html
|
| @@ -11,8 +11,9 @@
|
| behaviors: [SwarmingBehaviors.CommonBehavior]
|
| in the creation of any Polymer element.
|
|
|
| - SwarmingBehaviors.CommonBehavior contains shared functions to ease
|
| - templating, such as _or() and _not().
|
| + SwarmingBehaviors.CommonBehavior contains shared methods to ease
|
| + templating, such as _or() and _not() as well as general utility methods
|
| + such as _getJsonAsync.
|
| -->
|
|
|
| <script>
|
| @@ -21,6 +22,38 @@
|
| // This behavior wraps up all the shared swarming functionality.
|
| SwarmingBehaviors.CommonBehavior = {
|
|
|
| + // _getJsonAsync makes an XHR to a url, parses the response as JSON
|
| + // and sticks the resulting object into the property with the name given
|
| + // by "bindTo". If busy is defined, the property with that name will be
|
| + // set to true while the request is in flight and false afterwards.
|
| + // request headers (e.g. authentication) and query params will be used if
|
| + // provided. Query params is an object like {String:Array<String>}. On
|
| + // error, bindTo will be set to false. It is not set to undefined
|
| + // because computed values in Polymer don't fire if a property is
|
| + // undefined. Clients should check that bindTo is not falsey.
|
| + _getJsonAsync: function(bindTo, url, busy, headers, params) {
|
| + if (!bindTo || !url) {
|
| + console.log("Need at least a polymer element to bind to and a url");
|
| + return;
|
| + }
|
| + if (busy) {
|
| + this.set(busy, true);
|
| + }
|
| + url = url + "?" + sk.query.fromParamSet(params);
|
| + sk.request("GET", url, "", headers).then(JSON.parse).then(function(json){
|
| + this.set(bindTo, json);
|
| + if (busy) {
|
| + this.set(busy, false);
|
| + }
|
| + }.bind(this)).catch(function(reason){
|
| + console.log("Reason for failure of request to " + url, reason);
|
| + this.set(bindTo, false);
|
| + if (busy) {
|
| + this.set(busy, false);
|
| + }
|
| + }.bind(this));
|
| + },
|
| +
|
| _not: function(a) {
|
| return !a;
|
| },
|
|
|