Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. export function Factory(getInstance?: boolean) {
  2.  
  3. return function (target: any) {
  4.  
  5. let construct: any = function (constructor: Function, args: any[]) {
  6. var model: any = function () {
  7. return constructor.apply(this, args);
  8. }
  9. Object.keys(constructor.prototype).forEach((key:string) => {
  10. Object.defineProperty(model.prototype, key, Object.getOwnPropertyDescriptor(constructor.prototype, key));
  11. });
  12. return new model();
  13. };
  14.  
  15. let componentConstructor: any = function (...args: any[]) {
  16. var newInstance = construct(target, args);
  17. if (componentConstructor.$inject) {
  18. componentConstructor.$inject.forEach((el: string, index: number) => {
  19. newInstance[el] = componentConstructor.injectors[index];
  20. });
  21. }
  22. return newInstance;
  23. };
  24.  
  25. componentConstructor.$inject = target.$inject;
  26. componentConstructor.prototype = target.prototype;
  27.  
  28. let factoryFunction: any = (...args: any[]) => {
  29. if (args.length) {
  30. componentConstructor.injectors = args;
  31. } else if (componentConstructor.$inject) {
  32. let $injector: any = angular.injector(['ng']);
  33. componentConstructor.injectors = [];
  34. componentConstructor.$inject.forEach((el: string) => {
  35. componentConstructor.injectors.push($injector.get(el));
  36. });
  37. }
  38. if (getInstance) {
  39. return new componentConstructor();
  40. } else {
  41. return componentConstructor
  42. }
  43. };
  44.  
  45. factoryFunction.$inject = target.$inject
  46.  
  47. return factoryFunction;
  48.  
  49. };
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement