Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import * as _ from 'lodash';
  2. import {Observable} from 'rxjs/Rx';
  3.  
  4. import {Component, Input, Output, EventEmitter, OnInit, OnDestroy} from '@angular/core';
  5. import {FormGroup} from '@angular/forms';
  6.  
  7. import {formConfig} from './toolbar.form-config';
  8. import {JobToolbarVm} from '../view-model/job-toolbar.vm';
  9. import {BroadcastService} from '../../../services/broadcast/broadcast.service';
  10.  
  11. @Component({
  12. selector: 'wk-job-toolbar',
  13. template: require('./toolbar.html'),
  14. })
  15. export class JobToolbarComponent implements OnInit, OnDestroy {
  16.  
  17. protected form: FormGroup;
  18.  
  19. @Input()
  20. toolbar: JobToolbarVm;
  21.  
  22. @Output()
  23. toolbarChanged = new EventEmitter<JobToolbarVm>();
  24.  
  25. @Output()
  26. refresh = new EventEmitter<string>();
  27.  
  28. constructor(private broadcast: BroadcastService) {
  29. }
  30.  
  31. ngOnInit() {
  32.  
  33. this.form = formConfig;
  34. this.form.setValue(this.toolbar, {onlySelf: true});
  35.  
  36. // This ALWAYS RUNS when the form loads, ie. on the job route
  37. console.log('FORM VALUE');
  38. console.log(JSON.stringify(this.form.value, null, 2));
  39.  
  40. this.form.valueChanges
  41. .debounceTime(2000)
  42. .subscribe(
  43. this.onFormChange.bind(this)
  44. );
  45. }
  46.  
  47. ngOnDestroy() {
  48. //this.form.valueChanges.unsubscribe();
  49. //this.onChanges.unsubscribe();
  50. //this.toolbarChanged.unsubscribe();
  51. //this.form = null;
  52. }
  53.  
  54. onFormChange(data: any) {
  55. // This runs whenever I go to a different route and then come back to this route
  56. // There is also a memory leak, because this method fires multiple times based on how
  57. // often I navigate away and come back to this route.
  58. // e.g. Navigate away and then back 5 times, then I see this log statement 5 times
  59. console.log('FORM VALUE2 - THIS KEEPS FIRING FOR EACH INSTANCE OF MY COMPOMENT');
  60. console.log(JSON.stringify(this.form.value, null, 2));
  61.  
  62. JobToolbarVm.fromJsonIntoInstance(data, this.toolbar);
  63.  
  64. this.onChanges('data-changed');
  65. }
  66.  
  67. onChanges($event: any) {
  68. console.log('onChanges: ' + $event);
  69. // console.log(this.toolbar);
  70.  
  71. // Send the toolbar object back out to the parent
  72. this.toolbarChanged.emit(this.toolbar);
  73.  
  74. // Broadcast an event that will be listened to by the list component so that it knows when to refresh the list
  75. this.broadcast.broadcast('job-admin-toolbar-changed', this.toolbar);
  76. }
  77. }
  78.  
  79. class JobToolbarComponent
  80.  
  81. private subscr:Subscription;
  82.  
  83. ngOnInit() {
  84. ...
  85. this.subscr = this.form.valueChanges ...
  86. ...
  87. }
  88.  
  89. ngOnDestroy() {
  90. this.subscr.unsubscribe();
  91. }
  92. }
  93.  
  94. export function AutoUnsubscribe(exclude = []) {
  95.  
  96. return function (constructor) {
  97.  
  98. const original = constructor.prototype.ngOnDestroy;
  99.  
  100. constructor.prototype.ngOnDestroy = function () {
  101. for (let prop in this) {
  102. const property = this[prop];
  103. if (!exclude.includes(prop)) {
  104. if (property && (typeof property.unsubscribe === "function")) {
  105. property.unsubscribe();
  106. }
  107. }
  108. }
  109. original && typeof original === 'function' && original.apply(this, arguments);
  110. };
  111. }
  112.  
  113. }
  114.  
  115. @AutoUnsubscribe()
  116. @Component({
  117. selector: 'account-login',
  118. templateUrl: './login.component.html',
  119. styleUrls: ['./login.component.scss']
  120. })
  121. export class LoginComponent implements OnInit {
  122.  
  123.  
  124. public submitWatcher: Subscription;
  125.  
  126. submit() {
  127. this.submitWatcher = this.authService.login(this.loginForm.getRawValue())
  128. .subscribe(res => {
  129. if (this.returnUrl) {
  130. this.router.navigate([this.returnUrl]);
  131. }
  132. else {
  133. this.router.navigate(['/special']);
  134. }
  135. }, (error) => {
  136. alert(JSON.stringify(error.data));
  137. });
  138. }
  139.  
  140. }
  141.  
  142. var sub = this.items.subscribe(snapshots => {
  143. console.log(snapshots);
  144. sub.unsubscribe();
  145. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement