Skip to content

Commit

Permalink
Merge pull request #4507 from Polymer/fix-4500
Browse files Browse the repository at this point in the history
Adds back the `beforeRegister` method.
  • Loading branch information
kevinpschaaf authored Apr 7, 2017
2 parents b69254a + bbfea90 commit 282c3af
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ configure the module name. The only supported declarative way set the module
id is to use `id`.
* <a name="breaking-getPropertyInfo"></a>`element.getPropertyInfo`: This api returned unexpected information some of the time and was rarely used.
* <a name="breaking-getNativePrototype"></a>`element.getNativePrototype`: Removed because it is no longer needed for internal code and was unused by users.
* <a name="breaking-beforeRegister"></a>`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.
* <a name="breaking-beforeRegister"></a>`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 `properties`, `observers`, and `is` properties. 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.
* <a name="breaking-attributeFollows"></a>`element.attributeFollows`: Removed due to disuse.
* <a name="breaking-classFollows"></a>`element.classFollows`: Removed due to disuse.
* <a name="breaking-copyOwnProperty"></a>`element.copyOwnProperty`: Removed due to disuse.
Expand Down
17 changes: 10 additions & 7 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}

Expand Down
36 changes: 35 additions & 1 deletion test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
Expand All @@ -235,6 +240,9 @@
ready: function() {
this._ensureAttribute('attr', true);
},
beforePropChanged1: function() {
this.beforePropChangedCalled = true;
},
propChanged1: function() {
this.propChanged1Called = true;
},
Expand All @@ -245,13 +253,19 @@

Polymer.registerBehavior2 ={
prop2: true,
beforeRegister: function() {
this.beforeRegisterCount++;
},
registered: function() {
this.registeredCount++;
}
};

Polymer.registerBehavior3 ={
prop3: true,
beforeRegister: function() {
this.beforeRegisterCount++;
},
registered: function() {
this.registeredCount++;
}
Expand All @@ -264,11 +278,14 @@
Polymer.registerBehavior3
],
prop4: true,

beforeRegister: function() {
this.beforeRegisterCount++;
},
registered: function() {
this.registeredCount++;
},

beforeRegisterCount: 0,
registeredCount: 0,

is: 'behavior-registered'
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 282c3af

Please sign in to comment.