Chromium Code Reviews| 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. On error, bindTo will be set to false. It is not set to | |
| 31 // undefined because computed values in Polymer don't fire if a property | |
| 32 // 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.
| |
| 33 _getJsonAsync: function(bindTo, url, busy, headers, params) { | |
| 34 if (!bindTo || !url) { | |
| 35 console.log("Need at least a polymer element to bind to and a url"); | |
| 36 return; | |
| 37 } | |
| 38 if (busy) { | |
| 39 this.set(busy, true); | |
| 40 } | |
| 41 url = url + "?" + sk.query.fromParamSet(params); | |
| 42 sk.request("GET", url, "", headers).then(JSON.parse).then(function(json) { | |
| 43 this.set(bindTo, json); | |
| 44 if (busy) { | |
| 45 this.set(busy, false); | |
| 46 } | |
| 47 }.bind(this)).catch(function(reason){ | |
| 48 console.log("Reason for failure of request to " + url, reason); | |
| 49 this.set(bindTo, false); | |
| 50 if (busy) { | |
| 51 this.set(busy, false); | |
| 52 } | |
| 53 }.bind(this)); | |
| 54 }, | |
| 55 | |
| 24 _not: function(a) { | 56 _not: function(a) { |
| 25 return !a; | 57 return !a; |
| 26 }, | 58 }, |
| 27 | 59 |
| 28 _or: function() { | 60 _or: function() { |
| 29 var result = false; | 61 var result = false; |
| 30 // can't use .foreach, as arguments isn't really an Array. | 62 // can't use .foreach, as arguments isn't really an Array. |
| 31 for (var i = 0; i < arguments.length; i++) { | 63 for (var i = 0; i < arguments.length; i++) { |
| 32 result = result || arguments[i]; | 64 result = result || arguments[i]; |
| 33 } | 65 } |
| 34 return result; | 66 return result; |
| 35 }, | 67 }, |
| 36 }; | 68 }; |
| 37 })(); | 69 })(); |
| 38 </script> | 70 </script> |
| OLD | NEW |