イベントドライバとコンピュータプログラム
13988 ワード
Historical Background
Most likely many of you who will read this article dot remember times of Fortran langage and computters that were fed with tons of punch cards to have some some june.
The main purpose of computters at time was to computte something really;that is not true anymore because computters are used for getting computtional relt very rarely at schools or scientific institions nowadays.
How did it work at time?If you wanted a computter to run a program you went to a shelf with your puthed cards,you found the drawer with the stack of the m,fed the m through card reader and the computer started your program.
First task was to ask for user input and once you filled them in the program computed the relt,printed it and ended.Easy,strate forward,「single thread」job.
Events Introducced
With the invent of GUI and Mouse this concept of load-run-end just couldn't work and work anymore as we need some infinite loop that would wait for user actions(mouse movement and clicks)and)and and and and and and the the procecess the process.
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(partof)program、the event listener、that something happend.Events ares ares are e generation as.starster.stant.
The event source is independent of event listeners and it geners events also if nobody-listens or event if there isのlistener defined.The view point of our infinite loop would be:“I’m informing everevethmotion”
The view point of the listener would be:「Let me know when user moves the mouse,I need to do something with it.」
イベントin Ext
The re are twomail“sorts”of events in Ext:DOM events and JavaScript、or software、events.
DOMイベント
Browsers that display(X)HTML pages already have our「infinite loop」that watch user actions and fires events if these actions.occurring on DOM elemens.Before Ext wered used install everentatements.
JavaScript Events
Now,DOM elemens are not only possible event sources;it is quite easure to implement source logic and event listener installation to any JavaScript oject.But,what could it be good for?
Imagine that You have a coplex component such as grid.If you had only DOM events,the handing of user actions such column move beemmely difficult.You would need to listen DOM element,processetc.If would be much easure if grid component would do all this dirty work for you and,after everrything 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 potent tial listeners what hat happened to it.The same is true for another Ext coponents.Form validation events,Panel exress/Trexe
How do I listen to events?
If you have an object of Ext class、for example Panel、and you need to some action when panel resizes you would install a listener to implement your action:
How do I create event source?
Events related functionlity is implemented in Ext.util.Observable class so if you want your extension to be and vent source just exted Observable.Also,if you exted a class alady descendant of Obsevable
Events fired by your extension are events fired by parent class(es)
Custom Events
I t happens overy Organization Chart clast and you miplement drag&drop assigment/disyssal of employet/from a position.worledfield.worledIndent drag&drop ast/dismisal of employete/from/from a position.wolt.world.worledfilt.world
We could listen to these events and the listeners could send e-mals to the employee informing hit he has been assigned to a position or disyssed from it.
We do it this way:
Note:We do not exted Observable directly here but Panel,what we exted,does.Panel’s inhersitance chain:Observable->Component->BoxComponent->Cont-Panel.
And in
When we
Summary event is a message sent(fired)by event source to inform listeners that something happend event source is an object that can fire events event listener is a function that is caled when event source fires an event to listen to events we use to create an event source we exted Enjoy firing your events and listening to them!
http://blog.extjs.eu/know-how/events-explained/
Most likely many of you who will read this article dot remember times of Fortran langage and computters that were fed with tons of punch cards to have some some june.
The main purpose of computters at time was to computte something really;that is not true anymore because computters are used for getting computtional relt very rarely at schools or scientific institions nowadays.
How did it work at time?If you wanted a computter to run a program you went to a shelf with your puthed cards,you found the drawer with the stack of the m,fed the m through card reader and the computer started your program.
First task was to ask for user input and once you filled them in the program computed the relt,printed it and ended.Easy,strate forward,「single thread」job.
Events Introducced
With the invent of GUI and Mouse this concept of load-run-end just couldn't work and work anymore as we need some infinite loop that would wait for user actions(mouse movement and clicks)and)and and and and and and the the procecess the process.
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(partof)program、the event listener、that something happend.Events ares ares are e generation as.starster.stant.
The event source is independent of event listeners and it geners events also if nobody-listens or event if there isのlistener defined.The view point of our infinite loop would be:“I’m informing everevethmotion”
The view point of the listener would be:「Let me know when user moves the mouse,I need to do something with it.」
イベントin Ext
The re are twomail“sorts”of events in Ext:DOM events and JavaScript、or software、events.
DOMイベント
Browsers that display(X)HTML pages already have our「infinite loop」that watch user actions and fires events if these actions.occurring on DOM elemens.Before Ext wered used install everentatements.
<div
id
=
"mydiv"
onclick
=
"alert('You clicked me')"
>
Click me!</
div
>
Ext.Element wraps DOM elemens together with their events so now we install the same event handles 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 elemens are not only possible event sources;it is quite easure to implement source logic and event listener installation to any JavaScript oject.But,what could it be good for?
Imagine that You have a coplex component such as grid.If you had only DOM events,the handing of user actions such column move beemmely difficult.You would need to listen DOM element,processetc.If would be much easure if grid component would do all this dirty work for you and,after everrything 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 potent tial listeners what hat happened to it.The same is true for another Ext coponents.Form validation events,Panel exress/Trexe
How do I listen to events?
If you have an object of Ext class、for example Panel、and you need to 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 caled so you can doyour actions.How do I create event source?
Events related functionlity is implemented in Ext.util.Observable class so if you want your extension to be and vent source just exted Observable.Also,if you exted a class alady descendant of Obsevable
Events fired by your extension are events fired by parent class(es)
Custom Events
I t happens overy Organization Chart clast and you miplement drag&drop assigment/disyssal of employet/from a position.worledfield.worledIndent drag&drop ast/dismisal of employete/from/from a position.wolt.world.worledfilt.world
We could listen to these events and the listeners could send e-mals to the employee informing hit he has been assigned to a position or disyssed 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 clast that we are going to fire our new events it can do necessary setup.Note:We do not exted Observable directly here but Panel,what we exted,does.Panel’s inhersitance chain:Observable->Component->BoxComponent->Cont-Panel.
And in
assign
and dismiss
functions we fire our events after all assign/disys job has been done with signature of our chece.When we
fireEvent
、Observable look s if there re re some listeners to this event and cals all listeners with argments we supplied in fireEvent
cal.If there isのlistener just doth nothing.Summary
on
function to install an event listener Observable
class、addEvents
and fireEvent
http://blog.extjs.eu/know-how/events-explained/