| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | 2 Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 Use of this source code is governed under the Apache License, Version 2.0 | 3 Use of this source code is governed under the Apache License, Version 2.0 |
| 4 that can be found in the LICENSE file. | 4 that can be found in the LICENSE file. |
| 5 | 5 |
| 6 It contains the definition of the following Behaviors: | 6 It contains the definition of the following Behaviors: |
| 7 | 7 |
| 8 SwarmingBehaviors.CommonBehavior | 8 SwarmingBehaviors.CommonBehavior |
| 9 | 9 |
| 10 To use it, include | 10 To use it, include |
| 11 behaviors: [SwarmingBehaviors.CommonBehavior] | 11 behaviors: [SwarmingBehaviors.CommonBehavior] |
| 12 in the creation of any Polymer element. | 12 in the creation of any Polymer element. |
| 13 | 13 |
| 14 SwarmingBehaviors.CommonBehavior contains shared functions to ease | 14 SwarmingBehaviors.CommonBehavior contains shared methods to ease |
| 15 templating, such as _or() and _not(). | 15 templating, such as _or() and _not() as well as general utility methods |
| 16 such as _getJsonAsync. |
| 16 --> | 17 --> |
| 17 | 18 |
| 18 <script> | 19 <script> |
| 19 window.SwarmingBehaviors = window.SwarmingBehaviors || {}; | 20 window.SwarmingBehaviors = window.SwarmingBehaviors || {}; |
| 20 (function(){ | 21 (function(){ |
| 21 // This behavior wraps up all the shared swarming functionality. | 22 // This behavior wraps up all the shared swarming functionality. |
| 22 SwarmingBehaviors.CommonBehavior = { | 23 SwarmingBehaviors.CommonBehavior = { |
| 23 | 24 |
| 25 // _getJsonAsync makes an XHR to a url, parses the response as JSON |
| 26 // and sticks the resulting object into the property with the name given |
| 27 // by "bindTo". If busy is defined, the property with that name will be |
| 28 // set to true while the request is in flight and false afterwards. |
| 29 // request headers (e.g. authentication) and query params will be used if |
| 30 // provided. Query params is an object like {String:Array<String>}. On |
| 31 // error, bindTo will be set to false. It is not set to undefined |
| 32 // because computed values in Polymer don't fire if a property is |
| 33 // undefined. Clients should check that bindTo is not falsey. |
| 34 _getJsonAsync: function(bindTo, url, busy, headers, params) { |
| 35 if (!bindTo || !url) { |
| 36 console.log("Need at least a polymer element to bind to and a url"); |
| 37 return; |
| 38 } |
| 39 if (busy) { |
| 40 this.set(busy, true); |
| 41 } |
| 42 url = url + "?" + sk.query.fromParamSet(params); |
| 43 sk.request("GET", url, "", headers).then(JSON.parse).then(function(json)
{ |
| 44 this.set(bindTo, json); |
| 45 if (busy) { |
| 46 this.set(busy, false); |
| 47 } |
| 48 }.bind(this)).catch(function(reason){ |
| 49 console.log("Reason for failure of request to " + url, reason); |
| 50 this.set(bindTo, false); |
| 51 if (busy) { |
| 52 this.set(busy, false); |
| 53 } |
| 54 }.bind(this)); |
| 55 }, |
| 56 |
| 24 _not: function(a) { | 57 _not: function(a) { |
| 25 return !a; | 58 return !a; |
| 26 }, | 59 }, |
| 27 | 60 |
| 28 _or: function() { | 61 _or: function() { |
| 29 var result = false; | 62 var result = false; |
| 30 // can't use .foreach, as arguments isn't really an Array. | 63 // can't use .foreach, as arguments isn't really an Array. |
| 31 for (var i = 0; i < arguments.length; i++) { | 64 for (var i = 0; i < arguments.length; i++) { |
| 32 result = result || arguments[i]; | 65 result = result || arguments[i]; |
| 33 } | 66 } |
| 34 return result; | 67 return result; |
| 35 }, | 68 }, |
| 36 }; | 69 }; |
| 37 })(); | 70 })(); |
| 38 </script> | 71 </script> |
| OLD | NEW |