From 7bcf1007d3d444ecec271e866616e83480ac7375 Mon Sep 17 00:00:00 2001
From: Katherine Bailey <katherine@katbailey.net>
Date: Sun, 10 Apr 2011 13:45:02 -0700
Subject: [PATCH] Issue #769258: Ajax behaviors cannot be bound once on links

---
 misc/ajax.js |   27 +++++++++++++++++++++++++--
 1 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/misc/ajax.js b/misc/ajax.js
index fb03e2b..23e5ea2 100644
--- a/misc/ajax.js
+++ b/misc/ajax.js
@@ -29,6 +29,11 @@ Drupal.behaviors.AJAX = {
         }
         $(element_settings.selector).each(function () {
           element_settings.element = this;
+
+          // Check whether to bind behaviors using .one() instead of .bind().
+          if ($(this).hasClass('ajax-once')) {
+            element_settings.bindMethod = 'one';
+          }
           Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
         });
 
@@ -48,6 +53,10 @@ Drupal.behaviors.AJAX = {
         element_settings.url = $(this).attr('href');
         element_settings.event = 'click';
       }
+      // Check whether to bind behaviors using .one() instead of .bind().
+      if ($(this).hasClass('ajax-once')) {
+        element_settings.bindMethod = 'one';
+      }
       var base = $(this).attr('id');
       Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
     });
@@ -173,11 +182,25 @@ Drupal.ajax = function (base, element, element_settings) {
     type: 'POST'
   };
 
-  // Bind the ajaxSubmit function to the element event.
-  $(ajax.element).bind(element_settings.event, function (event) {
+  // By default, behaviors are bound using .bind().
+  if (!element_settings.bindMethod) {
+    element_settings.bindMethod = 'bind';
+  }
+ 
+   // Bind the handler to the element event using whichever bind method was set.
+  $(ajax.element)[element_settings.bindMethod](element_settings.event, function (event) {
     return ajax.eventResponse(this, event);
   });
 
+  // If the behavior was only bound once, the browser's default behavior needs
+  // to be prevented on subsequent events, so as to not follow links.
+  if (element_settings.bindMethod == 'one') {
+    $(ajax.element).bind(element_settings.event, function () {
+      return false;
+    });
+  }
+ 
+
   // If necessary, enable keyboard submission so that Ajax behaviors
   // can be triggered through keyboard input as well as e.g. a mousedown
   // action.
-- 
1.7.4.1

