eventtarget.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <html>
  2. <!--
  3. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  4. Use of this source code is governed by the Apache License, Version 2.0.
  5. See the COPYING file for details.
  6. -->
  7. <head>
  8. <title>Event Test</title>
  9. <script type="text/javascript" src="../base.js"></script>
  10. <script type="text/javascript">
  11. goog.require('goog.events.EventTarget');
  12. goog.require('goog.events.Event');
  13. </script>
  14. </head>
  15. <body>
  16. <script type="text/javascript">
  17. var preventDefault = false;
  18. var stopPropagation = false;
  19. function func1(e) {
  20. alert('func1');
  21. if (preventDefault) e.preventDefault();
  22. if (stopPropagation) e.stopPropagation();
  23. }
  24. function func2(e) {
  25. alert('func2');
  26. }
  27. function Something() { }
  28. goog.inherits(Something, goog.events.EventTarget);
  29. Something.prototype.DoSomething = function() {
  30. alert('Doing something');
  31. return this.dispatchEvent(new DemoEventObject('Wooo'));
  32. }
  33. function DemoEventObject(arg) {
  34. this.type = 'synth';
  35. this.arg = arg;
  36. }
  37. goog.inherits(DemoEventObject, goog.events.Event);
  38. var st = new Something();
  39. goog.events.listen(st, 'synth', func1, true);
  40. goog.events.listen(st, 'synth', func2, false);
  41. alert('Response = ' + st.DoSomething() + '\nShould be true');
  42. preventDefault = true;
  43. stopPropagation = false;
  44. alert('Response = ' + st.DoSomething() + '\nShould be false');
  45. preventDefault = false;
  46. stopPropagation = true;
  47. alert('Response = ' + st.DoSomething() + '\nShould be true and only func1 should have been called');
  48. preventDefault = true;
  49. stopPropagation = true;
  50. alert('Response = ' + st.DoSomething() + '\nShould be false and only func1 should have been called');
  51. </script>
  52. </body>
  53. </html>