Event driven programming with jQuery

These days, I have written a software, which extensively uses JavaScript. In this way, I wanted to call functions after the user has done specific actions. How can I do this?

The old way:
After each action, I have to write my functions, which should be called. This method works, of course. But it is not clear. It is more interesting to define my functions and than I want to define, after which actions these functions should be called. That should be done without changing the action. So I have tried to use jQuery for this way.

The new way:
jQuery offers the ability to abstract event handling between browsers. And this abstraction is done very well. It allows to expand possible events.

Imagine, you have an action called “action1”. I want to register some functions, which should be called in this action. So try this:

[source:js]
$(document).bind(“action1Call”,function1);
$(document).bind(“action1Call”,function2);
[/source]

It’s easy to call function1 and function2 in action1 now: Just write…

[source:js]
$(document).trigger(“action1Call”);
[/source]

… to the end of your action. That’s it. You can add new functions easily by binding them. Even no binding is possible (if there is no function, which should be called).

Ein Gedanke zu „Event driven programming with jQuery

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.