Offshoot from #345984: Client-side caching of administration menu:

Drupal.attachBehaviors() can be invoked more than once. In case the menu could not be loaded, another AJAX request will be issued.

In the other case, when the menu was loaded successfully, our behaviors needlessly run again on the menu.

Let's prevent this. Intelligently.

CommentFileSizeAuthor
#4 admin_menu.behaviors.patch1.54 KBsun

Comments

markus_petrux’s picture

Adding a class to the body?

Drupal.behaviors.adminMenu = function (context) {
  $('body:not(.admin-menu-processed)').addClass('admin-menu-processed').each(function() {
    // The actual code of the behavior goes here.
  });
};
sun’s picture

I tinkered pretty long about this issue...

Idea and conclusion:

Neither the DOM, nor a custom variable in Drupal.admin decides whether we ran AJAX already. The real indicator is the hash we get via Drupal.settings. This is especially important when considering that we might use further hashes for on-demand loaded sub-menus.

So what we want is:

- A registry of already requested hashes in Drupal.admin.hashes (property name could be a bit more descriptive though)

- Drupal.admin.getCache() always checks this registry before issuing another request. After successfully issuing an AJAX request, the given hash is added to the registry.

Remaining question is how we determine whether a request was successful. If I'm not mistaken, the onSuccess callback is always executed currently, even in case the server responds with a 404/403 or garbled output. You can certainly prove me wrong on that. :)

markus_petrux’s picture

As per the jQuery docs. $.ajax has several callbacks:

- success: A function to be called if the request succeeds.
- error: A function to be called if the request fails.
- complete: A function to be called when the request finishes (after success and error callbacks are executed).

So that complete callback can be used to clean up anything started via ajax request, and success callback to account for requests that succeeded, for example to update the hashes list. :)

sun’s picture

Status: Active » Needs review
StatusFileSize
new1.54 KB

Untested.

sun’s picture

Status: Needs review » Fixed

Tested and committed to all branches.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.