Home     |      Learn AJAX     |      Forum     |      Register     |      Submit Resource     |      Submit Article      |      Contact Us

Home AJAX-Tips
 

Target Events in Other Objects

 

Objects will always be paired with events. These two elements in a function are very essential in an Ajax based application especially when users wanted to provide additional actions. If the object or the event is not in the function, the function will be limited and will provide limited output.


But the implementation of events on objects is restricted. The restriction is basically on the idea that the set of events should only be implemented on specific objects. This is done to prevent confusion in the application. If the event is implemented in every object, the application will basically end up a mess.


However, there are situations wherein certain events in the Ajax based application might be applied to another object. Developers don't want to implement the said events across the application, only that there are situations where additional events can be implemented.


Copying as the Best Option


Like many tricks in Ajax and JavaScript, the best way to implement something restricted to a specific function is to copy the elements. This is no different to events - simply copy the needed events and attach them to the targeted object. It's simple and should work without any problem.


The main advantage of this technique is in its simplicity. However, there are some consequences developers have to be aware when using copy-paste function for events. These consequences will require developers to make some changes in the application to accommodate additional functions of events.


Overriding Loop


The biggest challenge related to copy-paste of functions is the looping behavior of events. When implementing an event in a specific function, the event will naturally loop since it will continue to check the functions and implement specific changes when needed. This behavior is expected but when the same event has the same loop in another object, a problem in performance could arise.


There are two implications in overriding loop: you should implement the function once or else the application will not work as expected and you'll have to time the implementation without dragging the previous events. This is actually a combination of delaying the function and using false returns in order to prevent the event from looping with the objective.


Focusing on Objects


Implementing the same events in more than one function can easily confuse developers into thinking that the function will be broken down into pieces. Part of this is true since the events will become another entity as it will be implemented in another function. But if you take a look at the event as a whole, you'll realize that you're simply copying a piece of information and tweaking it for additional use. This means the objects that will receive the function should be considered as well when implementing the events. Ignoring this fact could cause the application to fail since precautions on changing the objects to work well with the events were not made.


Implementing the same event in the same application is possible. Developers just have to make sure the new event doesn't loop and the objects were made to be receptive of the non-looping event.



Read Next: Fine Tuning Server Side Code



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links