Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(register-element): add check for callback being own property of opts
Browse files Browse the repository at this point in the history
Using other component libraries, like Polymer, alongside Angular2+zones would
cause errors because the descriptor was undefined after calling getOwnPropertyDescriptor
on objects which had inherited callbacks that were not their own properties.

Fixes #52
  • Loading branch information
jeffbcross committed May 20, 2015
1 parent a442a32 commit 8bce00e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/patch/register-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ function apply() {
];

document.registerElement = function (name, opts) {
callbacks.forEach(function (callback) {
if (opts.prototype[callback]) {
var descriptor = Object.getOwnPropertyDescriptor(opts.prototype, callback);
if (descriptor.value) {
descriptor.value = global.zone.bind(descriptor.value || opts.prototype[callback]);
_redefineProperty(opts.prototype, callback, descriptor);
if (opts && opts.prototype) {
callbacks.forEach(function (callback) {
if (opts.prototype.hasOwnProperty(callback)) {
var descriptor = Object.getOwnPropertyDescriptor(opts.prototype, callback);
if (descriptor.value) {
descriptor.value = global.zone.bind(descriptor.value);
_redefineProperty(opts.prototype, callback, descriptor);
} else {
opts.prototype[callback] = global.zone.bind(opts.prototype[callback]);
}
} else if (opts.prototype[callback]) {
opts.prototype[callback] = global.zone.bind(opts.prototype[callback]);
}
}
});
});
}

return _registerElement.apply(document, [name, opts]);
};
}
Expand Down
29 changes: 29 additions & 0 deletions test/patch/registerElement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,33 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()
var elt = document.createElement('x-props-desc');
});


it('should check bind callback if not own property', function (done) {
testZone.run(function() {
var originalProto = {
createdCallback: checkZone
};

var secondaryProto = Object.create(originalProto);
expect(secondaryProto.createdCallback).toBe(originalProto.createdCallback);

document.registerElement('x-inherited-callback', { prototype: secondaryProto });
expect(secondaryProto.createdCallback).not.toBe(originalProto.createdCallback);

function checkZone() {
expect(window.zone).toBeDirectChildOf(testZone);
done();
}

var elt = document.createElement('x-inherited-callback');
});
});


it('should not throw if no options passed to registerElement', function () {
expect(function() {
document.registerElement('x-no-opts');
}).not.toThrow();
});

}));

0 comments on commit 8bce00e

Please sign in to comment.