Created
February 5, 2017 06:07
-
-
Save miguel-leon/aeaaa616b0b87c35fec1c785b20a21de to your computer and use it in GitHub Desktop.
Sort of decorator so that the Attribute class constructor function is obtainable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('ng-attribute', []) | |
// Horrible hack for obtaining directive class `Attribute` constructor function. | |
// There is a nuisance in AngularJS in that the `constructor` property was not retained in the prototype. | |
.directive('attributesDecorator', function () { | |
// constructor extracted from angular.js | |
function Attributes(element, attributesToCopy) { | |
if (attributesToCopy) { | |
var keys = Object.keys(attributesToCopy); | |
var i, l, key; | |
for (i = 0, l = keys.length; i < l; i++) { | |
key = keys[i]; | |
this[key] = attributesToCopy[key]; | |
} | |
} | |
else { | |
this.$attr = {}; | |
} | |
this.$$element = element; | |
} | |
return { | |
template: function (_, attrs) { | |
Attributes.prototype = Object.getPrototypeOf(attrs); | |
Object.defineProperty(Attributes.prototype, 'constructor', { | |
writable: true, enumerable: false, configurable: true, | |
value: Attributes | |
}); | |
} | |
}; | |
}) | |
.run(function ($compile) { | |
$compile('<attributes-decorator>'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment