Chromium Code Reviews| 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..598b0c8e00d404418b975f501b0b8e47e555ce97 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,37 @@ |
| // 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. 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. |
|
jcgregorio
2016/08/24 20:39:07
Note that params are passed in as an obj.
kjlubick
2016/08/25 12:39:10
Done.
|
| + _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; |
| }, |