Advertisement
Guest User

Untitled

a guest
May 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. // Import the core angular services.
  2. import { Component } from "@angular/core";
  3. import { Observable } from "rxjs/Observable";
  4.  
  5. // NOTE: Loading RxJS operators for SIDE-EFFECTS only.
  6. import "rxjs/add/observable/of";
  7.  
  8.  
  9. // I provide the root component of the application.
  10. @Component({
  11. selector: "my-app",
  12. template:
  13. `
  14. From Observable: {{ someNumericProp }}
  15. `
  16. })
  17. export class AppComponent {
  18.  
  19. // Notice that we are explicitly declaring this property as a number. As such, the
  20. // TypeScript transpiler should warn us when / if we try to store a non-numeric
  21. // value into this property.
  22. public someNumericProp: number;
  23.  
  24.  
  25. // I initialize the component.
  26. constructor() {
  27.  
  28. this.someNumericProp = 0;
  29.  
  30. this.testControl();
  31. this.testA();
  32. this.testB();
  33. this.testC();
  34. this.testD();
  35.  
  36. // Logging out the current object so we can see what is on the instance
  37. // and what is on the prototype.
  38. console.log( this );
  39.  
  40. }
  41.  
  42.  
  43. // ---
  44. // PUBLIC METHODS.
  45. // ---
  46.  
  47.  
  48. // In this "control" test, I'm using my older (and favorite) approach where I would
  49. // have to use the .bind() operator in order to ensure that the subscribe callback
  50. // had access to the appropriate THIS scope. HOWEVER, we have to be careful because
  51. // using the .bind() method BREAKS ALL TYPE-SAFETY CHECKS.
  52. public testControl() : void {
  53.  
  54. Observable
  55. .of<number>( 1 )
  56. .subscribe( handleSubscribe.bind( this ) ) // <-- Using .bind( this )
  57. ;
  58.  
  59. function handleSubscribe( value: number ) : void {
  60.  
  61. this.someNumericProp = "value"; // <-- Type incompatibility.
  62.  
  63. }
  64.  
  65. }
  66.  
  67.  
  68. // In this test, we're maintaining proper type-checking by using the fat-arrow
  69. // syntax. However, in order to do that, we've switched over to an inline function
  70. // expression, which I don't love.
  71. // --
  72. // NOTE: The fat-arrow keeps the proper THIS reference while allowing type-checking.
  73. public testA() : void {
  74.  
  75. Observable
  76. .of<number>( 1 )
  77. .subscribe(
  78. ( value: number ) : void => {
  79.  
  80. this.someNumericProp = "value"; // <-- Type incompatibility.
  81.  
  82. }
  83. )
  84. ;
  85.  
  86. }
  87.  
  88.  
  89. // In this test, we're keeping the fat-arrow function syntax; however, rather than
  90. // using an inline function expression, we're moving to a variable assignment. But,
  91. // since VARIABLE ASSIGNMENTS AREN'T HOISTED, we have to assign the fat-arrow
  92. // function before we use it. Which is ghetto fabulous.
  93. // --
  94. // NOTE: The fat-arrow keeps the proper THIS reference while allowing type-checking.
  95. public testB() : void {
  96.  
  97. var handleSubscribe = ( value: number ) : void => {
  98.  
  99. this.someNumericProp = "value"; // <-- Type incompatibility.
  100.  
  101. };
  102.  
  103. Observable
  104. .of<number>( 1 )
  105. .subscribe( handleSubscribe )
  106. ;
  107.  
  108. }
  109.  
  110.  
  111. // In this test, we're creating and consuming an INSTANCE METHOD that was defined
  112. // using the fat-arrow syntax. Unfortunately, to use this approach, the subscribe
  113. // handler is relatively far away from the method that is consuming it.
  114. public testC() : void {
  115.  
  116. Observable
  117. .of<number>( 1 )
  118. .subscribe( this.testC_handleSubscribe )
  119. ;
  120.  
  121. }
  122.  
  123. // CAUTION: Since this is a property assignment, NOT a function declaration, this
  124. // function is being set on the INSTANCE and NOT ON THE PROTOTYPE.
  125. // --
  126. // NOTE: The fat-arrow keeps the proper THIS reference while allowing type-checking.
  127. public testC_handleSubscribe = ( value: number ) : void => {
  128.  
  129. this.someNumericProp = "value"; // <-- Type incompatibility.
  130.  
  131. }
  132.  
  133.  
  134. // IN this test, I'm going back to my older (and preferred style) in which the
  135. // callback is defined below its usage (and hoisted). However, rather than using
  136. // the .bind() method, we're using a closed-over "self" reference to maintain a
  137. // proper, type-safe reference to the THIS scope.
  138. public testD() : void {
  139.  
  140. var self = this;
  141.  
  142. Observable
  143. .of<number>( 1 )
  144. .subscribe( handleSubscribe )
  145. ;
  146.  
  147. function handleSubscribe( value: number ) : void {
  148.  
  149. self.someNumericProp = "value"; // <-- Type incompatibility.
  150.  
  151. }
  152.  
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement