OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 /** |
| 4 * Automatically extend using objects referenced in `behaviors` array. |
| 5 * |
| 6 * someBehaviorObject = { |
| 7 * accessors: { |
| 8 * value: {type: Number, observer: '_numberChanged'} |
| 9 * }, |
| 10 * observers: [ |
| 11 * // ... |
| 12 * ], |
| 13 * ready: function() { |
| 14 * // called before prototoype's ready |
| 15 * }, |
| 16 * _numberChanged: function() {} |
| 17 * }; |
| 18 * |
| 19 * Polymer({ |
| 20 * |
| 21 * behaviors: [ |
| 22 * someBehaviorObject |
| 23 * ] |
| 24 * |
| 25 * ... |
| 26 * |
| 27 * }); |
| 28 * |
| 29 * @class base feature: behaviors |
| 30 */ |
| 31 |
| 32 Polymer.Base._addFeature({ |
| 33 |
| 34 behaviors: [], |
| 35 |
| 36 _prepBehaviors: function() { |
| 37 this._flattenBehaviors(); |
| 38 this._prepBehavior(this); |
| 39 this.behaviors.forEach(function(b) { |
| 40 this._mixinBehavior(b); |
| 41 this._prepBehavior(b); |
| 42 }, this); |
| 43 }, |
| 44 |
| 45 _flattenBehaviors: function() { |
| 46 var flat = []; |
| 47 this.behaviors.forEach(function(b) { |
| 48 if (!b) { |
| 49 console.warn('Polymer: undefined behavior in [' + this.is + ']'); |
| 50 } else if (b instanceof Array) { |
| 51 flat = flat.concat(b); |
| 52 } else { |
| 53 flat.push(b); |
| 54 } |
| 55 }, this); |
| 56 this.behaviors = flat; |
| 57 }, |
| 58 |
| 59 _mixinBehavior: function(b) { |
| 60 Object.getOwnPropertyNames(b).forEach(function(n) { |
| 61 switch (n) { |
| 62 case 'registered': |
| 63 case 'properties': |
| 64 case 'observers': |
| 65 case 'listeners': |
| 66 case 'keyPresses': |
| 67 case 'hostAttributes': |
| 68 case 'created': |
| 69 case 'attached': |
| 70 case 'detached': |
| 71 case 'attributeChanged': |
| 72 case 'configure': |
| 73 case 'ready': |
| 74 break; |
| 75 default: |
| 76 this.copyOwnProperty(n, b, this); |
| 77 break; |
| 78 } |
| 79 }, this); |
| 80 }, |
| 81 |
| 82 _doBehavior: function(name, args) { |
| 83 this.behaviors.forEach(function(b) { |
| 84 this._invokeBehavior(b, name, args); |
| 85 }, this); |
| 86 this._invokeBehavior(this, name, args); |
| 87 }, |
| 88 |
| 89 _invokeBehavior: function(b, name, args) { |
| 90 var fn = b[name]; |
| 91 if (fn) { |
| 92 fn.apply(this, args || Polymer.nar); |
| 93 } |
| 94 }, |
| 95 |
| 96 _marshalBehaviors: function() { |
| 97 this.behaviors.forEach(function(b) { |
| 98 this._marshalBehavior(b); |
| 99 }, this); |
| 100 this._marshalBehavior(this); |
| 101 } |
| 102 |
| 103 }); |
| 104 |
OLD | NEW |