5pm - project management on time

Java Script form handler class. Part 2: implementing the locking feature. Jun 24, 2008

In part I I've showed how I use my class FormHandler for processing hot keys in client-side forms. In this post I'm going to demonstrate the locking feature implemented in FormHandler. You may download an archive with all tutorial files here: locks.zip.

Locking is necessary when you want to avoid executing some code while another code is being executed. Imagine, user clicks the Entery key on a product order form. You send an AJAX request to a server with order details. You don't want the user make another order by pressing the Enter key for the second time. What you want to do is to lock the form until the AJAX request is complete. In this case it's good to use the locking feature.

I have defined 3 methods in FormHandler class:

  • setLock(Name) – add a lock with a specified name
  • getLock(Name) – check whether a lock with a specified name exists
  • removeLock(Name) – deletes a lock with a specified name

These methods have very simple imlementation. They use MooTools Hash class.

  1. this.locks = new Hash();
  2. ..
  3. getLock: function( Name )
  4. {
  5. return this.locks.has(Name);
  6. }

After that I can use these methods in my form classes extending FormHandler this way:

  1. var KeyTester = new Class({
  2. Extends: FormHandler,
  3. initialize: function()
  4. {
  5. this.parent({
  6. keyMap: {
  7. 'LoginForm:enter': this.login.bind(this)
  8. }
  9. });
  10. },
  11. login: function()
  12. {
  13. if (this.getLock('login'))
  14. {
  15. alert('The form is locked. Processing request...');
  16. return;
  17. }
  18. this.setLock('login')
  19. alert('Login form submitted!');
  20. }
  21. });

When user presses the Enter key for the first time, the handler class sets the lock «login», that will not allow the method code to be executed for the seocond time. You may look how it work here.

I widely use this method in my applications. It is simple, useful and it really saves time. Sometimes I develop complex pages where user can edit many objects on a same page. In such cases I assign dynamic names to locks, matching object identifiers.

In a next post I will go into detail on how I implemented the simple loading indicators support.

Comments

This post has no comments.

Post a comment

  • Only these tags are allowed: <a>, <strong>
  • Preview Post
Mployd Employer Advertise here Boxed CSS Advertise here

RSS feeds

  • Applications
  • Job offers
  • Blog