Advertisement
Tobio

Untitled

Jan 2nd, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html lang="en">
  3.         <head>
  4.                 <title>JavaScript Patterns</title>
  5.                 <meta charset="utf-8">
  6.         </head>
  7.         <body>
  8.                 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  9.                 <script>
  10.                         /* Title: Custom events using .on() and .off()
  11.                          * Description: A really tiny pub/sub implementation for jQuery 1.7 using the two new methods(since jQuery 1.7): .on() and .off().
  12.                          */
  13.  
  14.                         /* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
  15.                          * http://benalman.com/
  16.                          * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
  17. /*
  18. Nguồn tài liệu:
  19.  http://learn.jquery.com/plugins/basic-plugin-creation/
  20.  http://stackoverflow.com/questions/3796788/whats-the-apply-jquery-function : .apply là để gọi hàm với 1 mảng các tham số
  21.  http://api.jquery.com/on/ : thêm đối tượng lắng nghe sự kiện
  22.  http://api.jquery.com/off/ : hủy lắng nghe sự kiện khỏi 1 đối tượng
  23.  http://api.jquery.com/trigger/: tạo event
  24.  
  25. Cái này nó viết ko theo hướng đối tượng, muốn làm  hướng đối tượng thì phải tạo 2 object như bên dưới, cái này chỉ là khuôn mẫu chung, đọc xong chế biến lại
  26. XEM CHƯƠNG 2 CUỐN HEAD FIRST DESIGN PATTERN NÓ SẼ GIẢI THÍCH
  27. Subject: đối tượng quan sát,
  28. {
  29.     var observers = [] : mảng lưu các phần tử lắng nghe sự kiện của thằng Subject
  30.     registerObserver(o) : add o vào mảng observers tương tự hàm addListener, thằng nào lắng nghe nó thì add vào mảng observers
  31.     removeObbrserver(o) : xóa thằng o khỏi mảng observers, ko lắng nghe sự kiện nữa
  32.     notifyObrserver() : duyệt mảng observers, gọi phương thức getData() của từng thằng observers[i], 0 <= i < observers.length -1
  33. }
  34. Observer: tạm hiểu là thằng quan sát subject, subject thay đổi thì nó sẽ xử lý tương ứng
  35. {
  36.     getData(): hàm xử lý sự kiện, nếu có tham số thì thêm vào
  37. }
  38.  
  39. Hoặc trộn lẫn chế biến thế nào tùy mày, để ý là mấy cái lớp UI bên Java với .NET cũng hoạt động tượng tự (addListener....)
  40.  
  41. Còn cái code dưới này thì cái subject là o, observer là arguments, code này nó truyền thẳng cái hàm vào luôn
  42. subsribe(o, arguments) : cho thằng agruments lắng nghe o
  43. unsubscribe(o, arguments) : bỏ thằng arguments ko cho lắng nghe o nữa
  44. publish(o, arguments) : phát sự kiện, jquery sẽ tìm những thằng lắng nghe o tương ứng rồi chạy hàm
  45.  
  46. */
  47.                         (function($) {
  48.  
  49.                           var o = $({});
  50.  
  51.                           $.subscribe = function() {
  52.                             o.on.apply(o, arguments);
  53.                           };
  54.  
  55.                           $.unsubscribe = function() {
  56.                             o.off.apply(o, arguments);
  57.                           };
  58.  
  59.                           $.publish = function() {
  60.                             o.trigger.apply(o, arguments);
  61.                           };
  62.  
  63.                         }(jQuery));
  64.  
  65. //Dưới này là ví dụ
  66.  
  67.                         // Usage:
  68.                         // Super-basic example:
  69.  
  70.                         function handle(e, a, b, c) {
  71.                           // `e` is the event object, you probably don't care about it.
  72.                           console.log(a + b + c);
  73.                         };
  74.  
  75.                         $.subscribe("/some/topic", handle);
  76.  
  77.                         $.publish("/some/topic", [ "a", "b", "c" ]);
  78.                         // logs: abc
  79.  
  80.                         $.unsubscribe("/some/topic", handle); // Unsubscribe just this handler
  81.  
  82.                         // Or:
  83.  
  84.                         $.subscribe("/some/topic", function(e, a, b, c) {
  85.                           console.log(a + b + c);
  86.                         });
  87.  
  88.                         $.publish("/some/topic", [ "a", "b", "c" ]);
  89.                         // logs: abc
  90.                         // Unsubscribe all handlers for this topic
  91.                         $.unsubscribe("/some/topic");
  92.  
  93.                         // References
  94.                         // https://gist.github.com/1321768
  95.                 </script>
  96.         </body>
  97. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement