From 7639cf815e3d3cf2d953a0e412f1cc1e2d84f0df Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Thu, 6 Apr 2017 10:45:26 -0700 Subject: [PATCH] Adds back the `beforeRegister` method. Users can no longer set the `is` property in this method; however, dynamic property effects can still be installed here. --- README.md | 2 +- lib/legacy/class.html | 17 ++++++++++------- test/unit/behaviors.html | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4b53774d92..4daca2f9c8 100644 --- a/README.md +++ b/README.md @@ -486,7 +486,7 @@ configure the module name. The only supported declarative way set the module id is to use `id`. * `element.getPropertyInfo`: This api returned unexpected information some of the time and was rarely used. * `element.getNativePrototype`: Removed because it is no longer needed for internal code and was unused by users. -* `element.beforeRegister`: This was originally added for metadata compatibility with ES6 classes. We now prefer users create ES6 classes by extending `Polymer.Element`, specifying metadata in the static `config` property. For legacy use via `Polymer({...})`, dynamic effects may now be added using the `registered` lifecycle method. +* `element.beforeRegister`: This was originally added for metadata compatibility with ES6 classes. We now prefer users create ES6 classes by extending `Polymer.Element`, specifying metadata in the static `config` property. For legacy use via `Polymer({...})`, dynamic effects may still be added by using `beforeRegister` but it is now equivalent to the `registered` lifecycle method. An element's `is` property cannot be set in `beforeRegister` as it could in Polymer 1.x. * `element.attributeFollows`: Removed due to disuse. * `element.classFollows`: Removed due to disuse. * `element.copyOwnProperty`: Removed due to disuse. diff --git a/lib/legacy/class.html b/lib/legacy/class.html index 108e3d1f4f..3929697886 100644 --- a/lib/legacy/class.html +++ b/lib/legacy/class.html @@ -162,6 +162,16 @@ _registered() { super._registered(); + /* NOTE: `beforeRegister` is called here for bc, but the behavior + is different than in 1.x. In 1.0, the method was called *after* + mixing prototypes together but *before* processing of meta-objects. + However, dynamic effects can still be set here and can be done either + in `beforeRegister` or `registered`. It is no longer possible to set + `is` in `beforeRegister` as you could in 1.x. + */ + if (info.beforeRegister) { + info.beforeRegister.call(Object.getPrototypeOf(this)); + } if (info.registered) { info.registered.call(Object.getPrototypeOf(this)); } @@ -311,13 +321,6 @@ LegacyElementMixin(HTMLElement)); // decorate klass with registration info klass.is = info.is; - // NOTE: while we could call `beforeRegister` here to maintain - // some BC, the state of the element at this point is not as it was in 1.0 - // In 1.0, the method was called *after* mixing prototypes together - // but before processing of meta-objects. Since this is now done - // in 1 step via `GenerateClassFromInfo`, this is no longer possible. - // However, *most* work (not setting `is`) that was previously done in - // `beforeRegister` should be possible in `registered`. return klass; } diff --git a/test/unit/behaviors.html b/test/unit/behaviors.html index 997a6ded74..24f44b5aea 100644 --- a/test/unit/behaviors.html +++ b/test/unit/behaviors.html @@ -224,6 +224,11 @@ }); Polymer.registerBehavior1 ={ + beforeRegister: function() { + this._createPropertyObserver('beforeProp', 'beforePropChanged1'); + this.beforeRegisterCount++; + this.beforeRegisterBehaviors = this.behaviors; + }, registered: function() { this._createPropertyObserver('prop', 'propChanged1'); this._createMethodObserver('propChanged2(prop)'); @@ -235,6 +240,9 @@ ready: function() { this._ensureAttribute('attr', true); }, + beforePropChanged1: function() { + this.beforePropChangedCalled = true; + }, propChanged1: function() { this.propChanged1Called = true; }, @@ -245,6 +253,9 @@ Polymer.registerBehavior2 ={ prop2: true, + beforeRegister: function() { + this.beforeRegisterCount++; + }, registered: function() { this.registeredCount++; } @@ -252,6 +263,9 @@ Polymer.registerBehavior3 ={ prop3: true, + beforeRegister: function() { + this.beforeRegisterCount++; + }, registered: function() { this.registeredCount++; } @@ -264,11 +278,14 @@ Polymer.registerBehavior3 ], prop4: true, - + beforeRegister: function() { + this.beforeRegisterCount++; + }, registered: function() { this.registeredCount++; }, + beforeRegisterCount: 0, registeredCount: 0, is: 'behavior-registered' @@ -366,6 +383,23 @@ }); +suite('behavior.beforeRegister', function() { + test('can install dynamic properties', function() { + var el = fixture('registered'); + assert.ok(el.$.content); + el.beforeProp = 43; + assert.isTrue(el.beforePropChangedCalled); + }); + + test('called once for each behavior with access to element prototype', function() { + var el = fixture('registered'); + assert.equal(el.beforeRegisterCount, 4); + assert.equal(el.beforeRegisterBehaviors.length, 3); + assert.equal(el.beforeRegisterBehaviors, el.behaviors); + }); + +}); + suite('multi-behaviors element', function() { var el;