`
dsotbs936
  • 浏览: 38030 次
  • 性别: Icon_minigender_1
  • 来自: 浙江
文章分类
社区版块
存档分类
最新评论

事件驱动与计算机程序

阅读更多

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

        this
.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!

 

http://blog.extjs.eu/know-how/events-explained/

分享到:
评论

相关推荐

    连接打印机提示“打印机驱动程序与你计算机上启动的阻止NT4.0驱动程序的策略不兼容”

    在 Windows 操作系统中,打印机驱动程序与 NT4.0 驱动程序的策略不兼容是由于打印机驱动程序与计算机上的阻止 NT4.0 驱动程序的策略冲突所致。这种冲突会导致打印机驱动程序无法安装,无法连接到打印机。 知识点二...

    HP5000打印机驱动程序

    这款打印机的驱动程序是连接硬件设备与计算机操作系统之间的桥梁,使得操作系统能够识别并控制打印机进行各种打印任务。 驱动程序在打印机操作中的作用: 1. **通信接口**:驱动程序负责处理数据传输,将计算机中...

    一个简单的驱动程序例程

    标题中的“一个简单的驱动程序例程”指的是在计算机操作系统中,编写用于控制硬件设备或与硬件交互的低级代码。这种代码通常被称为驱动程序,它充当操作系统与硬件之间的桥梁,使得操作系统能有效地管理和使用硬件...

    中断处理与设备驱动程序

    总的来说,中断处理与设备驱动程序在计算机系统中起着至关重要的作用,它们共同确保了系统能够有效地与硬件交互,及时响应各种事件,提高系统整体的效率和响应能力。在C语言环境下,开发者可以利用其简洁、高效的...

    最新万能USB驱动程序

    USB(Universal Serial Bus)驱动程序是计算机操作系统与USB设备间通信的关键组件。它允许系统识别并正确地与各种USB设备进行交互,如鼠标、键盘、打印机、扫描仪、移动硬盘、数码相机等。"最新万能USB驱动程序"通常...

    Ikey1000驱动程序

    Ikey1000驱动程序是针对特定硬件设备Ikey1000设计的软件组件,它在Windows 7和Windows 8操作系统上能够正常运行,确保该硬件设备与计算机系统之间的无缝交互。驱动程序在计算机系统中的作用至关重要,它们扮演着设备...

    驱动程序超级宝典

    驱动程序在计算机技术中扮演着至关重要的角色,它们是硬件设备与操作系统之间的桥梁,使得操作系统能够识别并控制硬件设备。"驱动程序超级宝典"很可能是一份详尽的指南,涵盖了驱动程序的各个方面,旨在帮助用户理解...

    索尼相机USB驱动程序

    【索尼相机USB驱动程序】是连接索尼_DSC-W50数码相机与计算机的重要软件组件,它使得用户能够通过USB接口方便地传输照片、视频或进行固件更新。USB驱动程序在计算机操作系统和硬件设备之间起着桥梁的作用,确保两者...

    CP5611驱动程序

    这款驱动程序是确保CP5611正常运行的关键软件组件,允许用户通过个人计算机进行编程、监控和诊断等操作。 CP5611驱动程序的主要功能包括: 1. **通信接口**:驱动程序提供了必要的接口,使CP5611能够与Windows操作...

    条码枪驱动程序

    条码枪驱动程序是连接条码扫描枪与计算机系统的关键组件,使得计算机能够识别和处理由扫描枪读取的条码数据。在IT领域,驱动程序通常被定义为硬件设备和操作系统之间的桥梁,它们提供了设备操作的具体指令,使得操作...

    JLINK驱动程序最新版

    驱动程序在计算机操作系统中扮演着至关重要的角色,它们是硬件设备与操作系统之间的桥梁。安装JLink驱动程序后,开发者可以通过USB或以太网将PC连接到目标板,实现对微控制器的实时调试,包括设置断点、查看和修改...

    rfid通用驱动程序

    标题“rfid通用驱动程序”指的是专为RFID读卡器设计的软件组件,这种驱动程序能够使计算机系统识别并控制各种不同型号的RFID读卡器,实现了硬件设备的兼容性,使得用户无需针对每种型号的读卡器安装特定的驱动,大大...

    Windows驱动程序开发++概念剖析

    在Windows操作系统中,驱动程序是计算机硬件与操作系统之间的重要桥梁,它们使得操作系统能够与硬件设备进行有效通信,实现对硬件的控制和管理。本篇内容将深入剖析Windows驱动程序开发的一些核心概念,帮助读者理解...

    win98驱动程序库

    在个人计算机领域,驱动程序是连接硬件设备与操作系统之间的重要桥梁,它使得操作系统能够识别并控制硬件设备,从而实现设备的正常工作。对于Windows 98这一经典操作系统而言,驱动程序的正确安装与更新同样至关重要...

    ADBMS1818驱动程序

    1. 确认操作系统版本:不同的操作系统可能需要不同版本的驱动,所以首先要确保驱动程序与操作系统兼容。 2. 安全下载:从可靠的源头获取驱动程序,避免下载过程中引入恶意软件。 3. 解压文件:将压缩包解压到本地...

    联想显示器驱动程序

    然后单击“浏览计算机以寻找驱动程序软件”按钮。 12. 选择“从计算机的设备驱动程序列表中选择”。 13. 将联想光盘放入光盘驱动器中,然后点击从磁盘安装按钮。 14. 通过点击浏览按钮,然后浏览并指向下述路径: X...

    USB Serial Controller驱动程序

    4. 系统兼容性:确认驱动程序与操作系统版本兼容,例如32位或64位。 总之,USB Serial Controller驱动程序在连接和管理通过USB接口的串行设备时起着至关重要的作用。理解其工作原理和安装过程有助于解决与USB到串行...

    USB转串口驱动程序\CP2101驱动

    USB转串口驱动程序是计算机硬件与串行通信设备间的重要桥梁,特别是在现代电子设备中,许多设备如Arduino、Raspberry Pi或者模块化的传感器等,都通过串口进行通信。CP2101是一款由Silicon Labs(芯科实验室)开发的...

    SafeNet微狗驱动程序

    SafeNet微狗驱动程序是连接微狗硬件与计算机操作系统之间的桥梁。它负责识别和配置硬件,使得操作系统和应用程序能够有效利用微狗的功能。例如,该驱动程序允许软件通过安全地存储和检索密钥来实现加密操作,从而...

Global site tag (gtag.js) - Google Analytics