2

I play around with Backbone.js (Great !) and want to synchronize my model data with the wordpress ajax api (3.6.1) on the server-side. Adapted to this http://addyosmani.github.io/backbone-fundamentals/#restful-persistence, here is my backbone example:

var new_posts;
var post;
(function($){
    var Post = Backbone.Model.extend({
        id: '',
        title: '',
    });
    var Posts = Backbone.Collection.extend({
        model: Post,
        url: APP.ajaxurl + '?action=postaction'  //APP.ajaxurl == http://localhost/wordpress/wp-admin/admin-ajax.php
    });
    new_posts = new Posts();
    new_posts.add([{'id':0, 'title':'Hermann Hesse'}, {'id':1, 'title':'Wolfgang v. Goethe'}, {'id':2, 'title':'Friedrich Schiller'}]);
    post = new_posts.get(2);
    post.set('title','Honore de Balzac');

    post.save();

}(jQuery));

I pick the second element (model) from a collection, change the 'title' - attribute and want to save it on the server side.

Backbone now creates with a 'PUT' a RESTFUL Request and append the index of the model inside the collection ('2') to the url:

http://localhost/wordpress/wp-admin/admin-ajax.php?action=postaction/2

...but i have NO IDEA how to handle the changed GET-Parameter 'action' (now: postaction/2), it seems that the standard wordpress approach with an action hook 'wp_ajax_' isn't the right one to handle this

add_action( 'wp_ajax_postaction', '<a function for handling>' );

Of course i cannot code an action hook for every possible parameter, which can be changed! Is there another wordpress approach to handle such PUT/Restful Request via Ajax ?

For any idea to this i thank in advance ! Thank you.

1 Answer 1

1

The dynamic wp_ajax_ hooks serve mostly organizational convenience on PHP side.

You could choose not to use it, then you would probably have to:

  • hook into admin_init
  • check that DOING_AJAX is true
  • check that request ($_REQUEST['action'] and rest of it) is your special kind of request

Then either process your request and die() or let anything else through to normal handling.

1
  • thank you so far, your approach works fine so far for my requirements :)
    – Alex
    Commented Sep 16, 2013 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.