Using AJAX in a theme

Hello. I am doing some work on a totally custom theme in Omeka 2.4 and I need to use AJAX to conditionally load some search facets.

Does Omeka have an AJAX method to route these calls though, like WordPress? If not, how would I go about accessing a function in a PHP file in my theme using jQuery’s $.ajax method? The below gives me a 404:

$.ajax({
  type: 'POST',
  dataType: 'html',
  url: '/ajax/facets.php',
  data: {
    action: 'listFacetItems',
    text: 'Test value to return'
  },
  success: function(data) {
    expandedFacet.body.html(data);
  }
});

Any suggestions or insight appreciated.

To make it work through AJAX, you’d need to also create a plugin – that’s how Omeka will know about a route to some URL. Without that, you’ll always get a 404.

If the conditions are not based on user interaction, you could probably build the same functionality directly into the theme. If they are, it’d be more complicated, but you could load all the content you need and them make javascript hide or show content based on those interactions.

OK. Would a plugin provide routing that can be used in the theme, or would it need to contain all the functionality?

It sounds like loading everything before conditionally showing/hiding it might be the better option at this point but we will need to handle AJAX requests at a later date.

Will themes in Omeka S be able to handle AJAX?

Nothing is built in to Omeka, but the JQuery stuff works fine in themes. They just need the plugin to get to the content.

The plugin would basically create a controller and view, where the view displays whatever content to the AJAX request. The VRA Core plugin does that a lot on the admin side, but the principles are the same.

If you check that out, look at the controller/AjaxController.php file, the action functions within it, and the views/admin/ajax/ files that correspond to the action functions. The javascript that does the AJAX is in views/admin/javascripts/vra-core.js

Omeka S will work similarly – you’ll just include JQuery as needed in your javascript files. At least on the admin side, there’s a lot of that currently going on for the sidebars.