e.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="utf-8">
  4. <title>Eve Use Case</title>
  5. <script src="eve.js"></script>
  6. <script>
  7. var log = function (text) {
  8. log.log = log.log || [];
  9. log.log.push(text);
  10. };
  11. window.onload = function () {
  12. document.getElementById("res").innerHTML = log.log.join("<br>");
  13. };
  14. </script>
  15. <script>
  16. var f1, f2, f3, f4;
  17. // setting up listeners
  18. eve.on("hit", f1 = function () {
  19. log(" I’m hit!");
  20. });
  21. eve.on("hit/face", f2 = function () {
  22. log(" Oh, my face!");
  23. });
  24. eve.on("hit/chest", f3 = function () {
  25. log(" Oh, my chest!");
  26. });
  27. eve.on("hit/*/leg", f4 = function () {
  28. log(" Ouch!");
  29. });
  30. eve.once("hit", function () {
  31. log(" You scoundrel!");
  32. })(-1);
  33. // fire events
  34. log("In your face!");
  35. eve("hit/face");
  36. // I’m hit!
  37. // Oh, my face!
  38. log("Take that!");
  39. // You can use “.” or “/” as delimiter
  40. eve("hit.chest.leg");
  41. // I’m hit!
  42. // Oh, my chest!
  43. // Ouch!
  44. // Unbinding
  45. log("");
  46. eve.unbind("hit", f3);
  47. log("Take that!");
  48. eve("hit.chest.leg");
  49. // I’m hit!
  50. // Ouch!
  51. // Unbinding by wildcard
  52. log("");
  53. eve.unbind("hit/*");
  54. log("In your face!");
  55. eve("hit.face");
  56. // I’m hit!
  57. log("Take that!");
  58. eve("hit.chest.leg");
  59. // I’m hit!
  60. </script>
  61. <pre id="res"></pre>
  62. </html>