事件 Events Explained

简介: Tutorial:Events Explained From Learn About the Ext JavaScript Library Jump to: navigation, search Summary: This tutorial will explain what events are and how they are used in Ext.

Tutorial:Events Explained

From Learn About the Ext JavaScript Library

Jump to: navigation, search
Summary: This tutorial will explain what events are and how they are used in Ext.
Author: Jozef Sakalos, aka Saki
Published: May 18, 2008
Ext Version: 2.0+
Languages: en.pngEnglish

Contents

[hide]

Historical Background

Most likely many of you who will read this article do not remember times of Fortran language and computers that were fed with tons of punch cards to have some some job done.

The main purpose of computers at that time was to compute something really; that is not true anymore because computers are used for getting computational result very rarely at schools or scientific institutions nowadays.

How did it work at that time? If you wanted a computer to run a program you went to a shelf with your punched cards, you found the drawer with the stack of them, fed them through card reader and the computer started your program.

First task was to ask for user inputs and once you filled them in the program computed the result, printed it and ended. Easy, straightforward, "single thread" job.

Events Introduced

With the invent of GUI and Mouse this concept of load-run-end just couldn't work anymore as we needed some infinite loop that would wait for user actions (mouse movement and clicks) and that would process them to execute the required actions.

I has became clear very soon that putting the code that processes these action directly in that loop is the route to nowhere so the Event driven programming was born.

Event Definition

Event is a message, a function call, generated by one (part of) program, the event source, that notifies another (part of) program, the event listener, that something happened. Events are generated as responses to user actions or to state changes of the event source.

The event source is independent of event listeners and it generates events also if nobody listens or even if there is no listener defined. The viewpoint of our infinite loop would be: "I'm informing everybody that user moved the mouse to position [x,y] and I do not care who listens, if anybody."

The viewpoint of the listener would be: "Let me know when user moves the mouse, I need to do something with it."

Events in Ext

There are two main "sorts" of events in Ext: DOM events and JavaScript, or software, events.

DOM Events

Browsers that display (X)HTML pages already have our "infinite loop" that watches user actions and fires events if these actions are occurring on DOM elements. Before Ext we were used to install event listeners on DOM elements this way:

<div id="mydiv" onclick="alert('You clicked me')">Click me!</div>

Ext.Element wraps DOM elements together with their events so now we install the same event handlers this way:

Ext.get('mydiv').on('click', function() {alert('You clicked me');});

It can be said that DOM events are "passed-through" from DOM through Ext.Element to listeners.

JavaScript Events

Now, DOM elements are not only possible event sources; it is quite easy to implement event source logic and event listener installation to any JavaScript object. But, what could it be good for?

Imagine that you have a complex component such as grid. If you had only DOM events, the handling of user actions such as column move would be extremely difficult. You would need to listen to DOM elements, process mouse clicks, moves, calculate from where to where the column has been moved, etc. If would be much easier if grid component would do all this dirty work for you and, after everything has be done, just informed you: "User moved column 3 to position 1."

That is exactly what grid does: it fires JavaScript events that inform potential listeners what has happened to it. The same is true for another Ext components. Form validation events, Panel resize events, Tree expand/collapse events can serve as examples, to name a few.

How do I listen to events?

If you have an object of Ext class, for example Panel, and you need to do some action when panel resizes you would install a listener to implement your action:

// create panel
var myPanel = new Ext.Panel({...});
 
// install resize event listener
myPanel.on('resize', function(panel, w, h) {
    alert('Panel resized to ' + w + 'x' + h);
});

From this point on, whenever the panel myPanel is resized your function is called so you can do your actions.

How do I create event source?

Events related functionality is implemented in Ext.util.Observable class so if you want your extension to be an event source just extend Observable. Also, if you extend a class that is already descendant of Observable (Panel, Grid, Form, Tree, etc), your extension is automatically the event source.

Events fired by your extension are events fired by parent class(es).

Custom Events

It happens very often that you need add new events, for example you create Employee class and Organization Chart class and you implement drag&drop assignment/dismissal of employee to/from a position. I would come handy to fire event assigned and dismissed, wouldn't it?

We could listen to these events and the listeners could send e-mails to the employee informing him that he has been assigned to a position or dismissed from it.

We do it this way:

  
  
OrgChart  =  Ext.extend(Ext.Panel,  {
    initComponent:
function() {
        
// call parent init component
        OrgChart.superclass.initComponent.apply(this, arguments);
 
        
// add custom events
        addEvents('assigned''dismissed');
    }

 
    ,assign:
function(employee, position) {
        
// do whatever is necessary to assign the employee to position
 
        
// fire assigned event
        this.fireEvent('assigned'this, employee, position);
    }

 
    ,dismiss:
function(empoyee, position) {
        
// do whatever is necessary to dismiss employee from position
 
        
// fire dismissed event
        this.fireEvent('dismissed'this, employee, position);
    }

}
);

In the initComponent function we inform Observable class that we are going to fire our new events so it can do all necessary setup.

Note: We do not extend Observable directly here but Panel, what we extend, does. Panel's inheritance chain is: Observable -> Component -> BoxComponent -> Container -> Panel.

And in assign and dismiss functions we fire our events after all assign/dismiss job has been done with signature (arguments) of our choice.

When we fireEvent, Observable looks if there are some listeners to this event and calls all listeners with arguments we supplied in fireEvent call. If there is no listener it just does nothing.

Summary

  • event is a message sent (fired) by an event source to inform listeners that something happened
  • event source is an object that can fire events
  • event listener is a function that is called when event source fires an event
  • to listen to events we use on function to install an event listener
  • to create an event source we extend Observable class, addEvents and fireEvent

Enjoy firing your events and listening to them!

Further Reading

目录
相关文章
|
1月前
|
Linux 网络架构
perf_event_open学习 —— design
perf_event_open学习 —— design
|
存储 机器学习/深度学习 人工智能
PTPCG: Efficient Document-level Event Extraction via Pseudo-Trigger-aware Pruned Complete Graph论文解读
据我们所知,我们目前的方法是第一项研究在DEE中使用某些论元作为伪触发词的效果的工作,我们设计了一个指标来帮助自动选择一组伪触发词。此外,这种度量也可用于度量DEE中带标注触发词的质量。
122 1
|
自然语言处理 Java 测试技术
Saliency as Evidence: Event Detection with Trigger Saliency Attribution 论文解读
事件检测(ED)是事件抽取的关键子任务,它试图识别文本中特定类型的事件触发词。尽管ED取得了重大进展,但现有方法通常遵循“一个模型适合所有类型”的方法,这种方法认为事件类型之间没有差异,通常会导致相当倾斜的性能。
69 0
|
存储 移动开发 自然语言处理
Document-Level event Extraction via human-like reading process 论文解读
文档级事件抽取(DEE)特别困难,因为它提出了两个挑战:论元分散和多事件。第一个挑战意味着一个事件记录的论元可能存在于文档中的不同句子中
90 0
|
机器学习/深度学习 人工智能 自然语言处理
DualCor: Event Causality Extraction with Event Argument Correlations论文解读
事件因果关系识别(ECI)是事件因果关系理解的重要任务,其目的是检测两个给定文本事件之间是否存在因果关系。然而,ECI任务忽略了关键的事件结构和因果关系组件信息
94 0
有趣的 events_statements_current 表问题
有趣的 events_statements_current 表问题
150 0
|
图形学
Unity【Timeline】- 使用Signal Track添加事件
Unity【Timeline】- 使用Signal Track添加事件
1245 0
Unity【Timeline】- 使用Signal Track添加事件
|
前端开发
Warning: This synthetic event is reused for performance reasons.
Warning: This synthetic event is reused for performance reasons.
509 0
Warning: This synthetic event is reused for performance reasons.