XF 2.1 How to call class action method via XF.ajax()?

asprin

Active member
I've the ajax call set up as following:

JavaScript:
var config = {
    method: 'post',
    action: "{{link('vendor/my-function')}}", // the route is set via ACP
    successCallback: XF.proxy(this, 'processResponse'),
    ajaxOptions: { skipDefault: true }
};

XF.ajax(
    config.method,
    config.action,
    {'f':selection},
    config.successCallback,
    config.ajaxOptions
);

But line 3 generating an invalid URL, something like the following:

i.e. spitting it out as is. How can make link() work inside a .js file?

For the record, I'm registering the event as per XF system...ie
JavaScript:
!(function($, window, document, _undefined) {
  'use strict';

  asprin.OnFormatSelection = XF.Event.newHandler({
    eventType: 'change',
    eventNameSpace: 'AsprinOnFormatSelection',

    init: function() {
      console.debug('handler initialized');
    },

    change: function(event) {
      // this is where my XF.ajax() goes
    },   
  });

  XF.Event.register('change', 'blah-blah', 'asprin.OnFormatSelection');
})(jQuery, window, document);
 
Solution
You can use XF.canonicalizeUrl('index.php?vendor/my-function'), or pass it through from the template (as an option) if hard-coding it isn't an option.
You can use XF.canonicalizeUrl('index.php?vendor/my-function'), or pass it through from the template (as an option) if hard-coding it isn't an option.
 
Solution
You can use XF.canonicalizeUrl('index.php?vendor/my-function'), or pass it through from the template (as an option) if hard-coding it isn't an option.
Thanks. That does the trick, although using the index.php as part of the URL seems ugly.

On another note, you mentioned passing the url from the template. You mean as a data- attribute on the element that is triggering the AJAX request?
 
Back
Top Bottom