ExtJS learning path: ExtJs event (Custom Event, on, eventManager) Example

Source: Internet
Author: User

Page code:

[Javascript]View plaincopyprint?
  1. <Body>
  2. <Link rel = "stylesheet" type = "text/css" href = ".. /.. /.. /.. /resources/css/ext-all.css "mce_href =" resources/css/ext-all.css "/>
  3. <Mce: script type = "text/javascript" src = ".. /.. /.. /.. /ext/ext-base.js "mce_src =" ext/ext-base.js "> </mce: script>
  4. <Mce: script type = "text/javascript" src = ".. /.. /.. /.. /ext/ext-all.js "mce_src =" ext/ext-all.js "> </mce: script>
  5. <Div style = "padding: 10px 10px 20px;" mce_style = "padding: 10px 10px 10px 20px;">
  6. Event examples, including user-defined events and Use of on, addListener, and EventManager.
  7. </Div>
  8. <Div id = "eventTestDiv" style = "padding: 10px 10px 20px; width: 150px; height: 50px; background-color: green;">
  9. Move the mouse over to me!
  10. </Div>
  11. <Br>
  12. <Div style = "padding: 10px 10px 20px;" mce_style = "padding: 10px 10px 10px 20px;">
  13. <Input type = "button" name = "button1" id = "button1" value = ""/>
  14. </Div>
  15. <Mce: script type = "text/javascript" src = "CustomEvent. js" mce_src = "CustomEvent. js"> </mce: script>
  16. </Body>

 

Js Code

[Javascript]View plaincopyprint?
  1. /*************************************** *
  2. * Use custom events
  3. */
  4. Var Person = function (){
  5. // Register an event. The event name is "say ".
  6. This. addEvents ("say ");
  7. // Register multiple events in another way
  8. This. addEvents ({
  9. "Run": true,
  10. "See": false
  11. });
  12. }
  13. // Event processing in Extjs can be performed only when the Ext. util. Observable class is inherited.
  14. Ext. extend (Person, Ext. util. Observable );
  15. // Define related execution methods
  16. Var speak = function (){
  17. Alert ("I can speak! ");
  18. }
  19. Var run = function (){
  20. Alert ("I can run! ");
  21. }
  22. Var see = function (){
  23. Alert ("I can see the word! ");
  24. }
  25. Var person = new Person ();
  26. // Bind the method say to the event say. The two names can be different.
  27. Person. on ("say", speak );
  28. Person. on ("run", run );
  29. Person. on ("see", see );
  30. // Trigger event. Only say and run are triggered here. The see event is not triggered.
  31. Person. fireEvent ("say ");
  32. Person. fireEvent ("run ");
  33. /*************************************** **/
  34. Ext. onReady (function (){
  35. // The returned Element object is not equivalent to document. getElementById (id)
  36. // Ext. getDom (id) is equivalent to document. getElementById (id)
  37. Var eventTestDiv = Ext. get ("eventTestDiv ");
  38. // Implemented through EventManager or obj. addListener. See the following example.
  39. Ext. EventManager. addListener (eventTestDiv, "mouseover", function (){
  40. Ext. Msg. alert ('info', 'Id: '+ this. Id +' <br> content: '+ this. innerHTML );
  41. });
  42. Ext. EventManager. addListener (eventTestDiv, "click", function (){
  43. Ext. Msg. alert ('info', 'Id: '+ this. Id
  44. + '<Br> name:' + this. name
  45. + '<Br> value:' + this. value );
  46. });
  47. /**
  48. In the example of addListener, the third parameter refers to the scope, and the fourth parameter is a configuration option.
  49. There are four configuration options:
  50. Delay {Number}: the time when the function execution is delayed after the event is triggered. (How long will the specified function be executed after the event is triggered );
  51. Scope {Object}: The scope of the event processing function execution. The context of the processing function "this. (If 3rd parameters are set, save them here );
  52. Single {Boolean}: true indicates that a processing function is added after the event is triggered. (Once an event is triggered, it will be removed and then triggered later );
  53. Buffer {Number}: If a millisecond is specified, the processing function is scheduled to be executed after Ext. util. DelayedTask is delayed. If the event is triggered again in that event, the original processor handle will not be enabled, but the new processor handle will be placed in its location.
  54. The following configurations are not used:
  55. Delegate {String}: A simple Selector Used to filter targets or find their children.
  56. StopEvent {Boolean}: true indicates that the event is blocked. That is, stop the propagation and stop the default action.
  57. PreventDefault {Boolean}: true indicates that the default action is blocked.
  58. StopPropagation {Boolean}: true indicates to prevent event propagation.
  59. Normalized {Boolean}: false indicates that the handler is sent to an original, unencapsulated browser object instead of a non-standard Ext. EventObject.
  60. */
  61. Var button1 = Ext. get ("button1 ");
  62. // AddListener is equivalent to on, and on is short for addListener.
  63. Button1.addListener ("click", function (){
  64. // Obtain the actually passed parameter values. The first two parameters are EventObject and Element, and the third parameter is the option object.
  65. Var msg = '<br/> obtain the optional parameter information: <br/> Number of optional parameters:' + arguments. length + "<br/> ";
  66. For (var I in arguments [2]) {
  67. Msg + = I + ":" + arguments [2] [I] + "<br/> ";
  68. }
  69. Ext. Msg. alert ('info', 'print eventTestDiv information: <br/> Id: '+ this. id
  70. + "<Br/>" + msg
  71. );
  72. }, EventTestDiv ,{
  73. // Event delay trigger event
  74. Delay: 500,
  75. // Whether to trigger only once
  76. Single: false,
  77. Msg: 'The append parameter'
  78. });
  79. /**
  80. Several event writing methods (addListener and on are equivalent ):
  81. Var fn = function (){
  82. // Add the logic to be processed.
  83. };
  84. // Method 1
  85. Button1.addListener ("click", fn, button1 );
  86. Button1.addListener ("mouseover", fn, button1 );
  87. // Method 2
  88. Button1.on ({
  89. Click: fn,
  90. Mouseover: fn,
  91. Scope: button1
  92. });
  93. // Method 3
  94. Button1.on ({
  95. Click :{ scope: button1, delay: 3000, fn: fn },
  96. Mouseover: {scope: button1, delay: 1000, fn: fn}
  97. });
  98. */
  99. })

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.