Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. /**
  2. * class-oriented design - OO
  3. */
  4.  
  5. // Parent class
  6. function Controller() {
  7. this.errors = [];
  8. };
  9.  
  10. Controller.prototype.showDialog = function(title,msg) {
  11. // display title & message to user in dialog
  12. };
  13.  
  14. Controller.prototype.success = function(msg) {
  15. this.showDialog( "Success", msg );
  16. };
  17.  
  18. Controller.prototype.failure = function(err) {
  19. this.errors.push( err );
  20. this.showDialog( "Error", err );
  21. };
  22.  
  23. // Child class
  24. function LoginController() {
  25. Controller.call( this );
  26. };
  27.  
  28. // Link child class to parent
  29. LoginController.prototype = Object.create( Controller.prototype );
  30.  
  31. LoginController.prototype.getUser = function() {
  32. return document.getElementById( "login_username" ).value;
  33. };
  34.  
  35. LoginController.prototype.getPassword = function() {
  36. return document.getElementById( "login_password" ).value;
  37. };
  38.  
  39. LoginController.prototype.validateEntry = function(user,pw) {
  40. user = user || this.getUser();
  41. pw = pw || this.getPassword();
  42. if (!(user && pw)) {
  43. return this.failure(
  44. "Please enter a username & password!"
  45. );
  46. }
  47. else if (user.length < 5) {
  48. return this.failure(
  49. "Password must be 5+ characters!"
  50. );
  51. }
  52. // got here? validated!
  53. return true;
  54. };
  55.  
  56. // Override to extend base `failure()`
  57. LoginController.prototype.failure = function(err) {
  58. // "super" call
  59. Controller.prototype.failure.call(
  60. this,
  61. "Login invalid: " + err
  62. );
  63. };
  64.  
  65. // Child class
  66. function AuthController(login) {
  67. Controller.call( this );
  68. // in addition to inheritance, we also need composition
  69. this.login = login;
  70. };
  71.  
  72. // Link child class to parent
  73. AuthController.prototype = Object.create( Controller.prototype );
  74.  
  75. AuthController.prototype.server = function(url,data) {
  76. return $.ajax( {
  77. url: url,
  78. data: data
  79. } );
  80. };
  81.  
  82. AuthController.prototype.checkAuth = function() {
  83. var user = this.login.getUser();
  84. var pw = this.login.getPassword();
  85. if (this.login.validateEntry( user, pw )) {
  86. this.server( "/check-auth",{
  87. user: user,
  88. pw: pw
  89. } )
  90. .then( this.success.bind( this ) )
  91. .fail( this.failure.bind( this ) );
  92. }
  93. };
  94.  
  95. // Override to extend base `success()`
  96. AuthController.prototype.success = function() {
  97. // "super" call
  98. Controller.prototype.success.call( this, "Authenticated!" );
  99. };
  100.  
  101. // Override to extend base `failure()`
  102. AuthController.prototype.failure = function(err) {
  103. // "super" call
  104. Controller.prototype.failure.call(
  105. this,
  106. "Auth Failed: " + err
  107. );
  108. };
  109.  
  110. var auth = new AuthController();
  111. auth.checkAuth(
  112. // in addition to inheritance, we also need composition
  113. new LoginController()
  114. );
  115.  
  116.  
  117. /**
  118. * behavior delegation design pattern - OLOO
  119. */
  120. var LoginController = {
  121. errors: [],
  122. getUser: function() {
  123. return document.getElementById(
  124. "login_username"
  125. ).value;
  126. },
  127. getPassword: function() {
  128. return document.getElementById(
  129. "login_password"
  130. ).value;
  131. },
  132. validateEntry: function(user,pw) {
  133. user = user || this.getUser();
  134. pw = pw || this.getPassword();
  135. if (!(user && pw)) {
  136. return this.failure(
  137. "Please enter a username & password!"
  138. );
  139. }
  140. else if (user.length < 5) {
  141. return this.failure(
  142. "Password must be 5+ characters!"
  143. );
  144. }
  145. // got here? validated!
  146. return true;
  147. },
  148. showDialog: function(title,msg) {
  149. // display success message to user in dialog
  150. },
  151. failure: function(err) {
  152. this.errors.push( err );
  153. this.showDialog( "Error", "Login invalid: " + err );
  154. }
  155. };
  156.  
  157. // Link `AuthController` to delegate to `LoginController`
  158. var AuthController = Object.create( LoginController );
  159. AuthController.errors = [];
  160. AuthController.checkAuth = function() {
  161. var user = this.getUser();
  162. var pw = this.getPassword();
  163. if (this.validateEntry( user, pw )) {
  164. this.server( "/check-auth",{
  165. user: user,
  166. pw: pw
  167. } )
  168. .then( this.accepted.bind( this ) )
  169. .fail( this.rejected.bind( this ) );
  170. }
  171. };
  172. AuthController.server = function(url,data) {
  173. return $.ajax( {
  174. url: url,
  175. data: data
  176. } );
  177. };
  178. AuthController.accepted = function() {
  179. this.showDialog( "Success", "Authenticated!" )
  180. };
  181. AuthController.rejected = function(err) {
  182. this.failure( "Auth Failed: " + err );
  183. };
  184. AuthController.checkAuth();//call
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement