Introducing the new AJAX Framework + Turbo Router

Hello everyone.

We are pleased to announce that included in v3 onwards, October CMS ships with a new AJAX framework that removes the need to include jQuery in your themes. The new framework is a drop-in replacement for the old one and includes ongoing support for both jQuery and Vanilla JS.

Library Components

The AJAX framework library contains four main components:

Component Description
AjaxRequest Includes javascript API with request object.
AjaxFramework Applies data attributes API to the DOM.
AjaxExtras Applies extra AJAX features to the DOM.
AjaxTurbo Applies the PJAX library to the DOM.

It is possible to import the framework as a module for build tools like webpack:

npm install --save octobercms

Then require it in your scripts:

var oc = require('octobercms');

The code for the AJAX Framework is public and can be found at the following GitHub repo:

The Turbo Router

The Turbo Router is an implementation of PJAX (push state and AJAX) that uses complementary techniques to dramatically reduce the amount of JavaScript needed to create modern websites. PJAX is now included in the frontend and backend areas by default and is a step forward in modernizing the platform.

PJAX changes the way the browser interacts with the application and uses a slightly different page lifecycle. Specifically, new events are needed to initialize JavaScript controls and controls should be disposable.

If your website or application is affected by this change, we recommend disabling PJAX by including the following META tag in your webpage layouts.


<head>
    <meta name="turbo-visit-control" content="disable" />
</head>

If there are affected plugins, you may also disable the turbo router in the backend with the backend.turbo_router setting.


// config/backend.php
//
'turbo_router' => false,

Plugin developers can disable PJAX on specific controllers by including the turboVisitControl property.


/**
 * @var bool turboVisitControl forces a reload
 */
public $turboVisitControl = 'disable';

We encourage you to learn more about introducing PJAX support by reading the documentation.

Just One Breaking Change

There is only one significant breaking change to the error function signature. The argument order has changed and this is an intentional break to make the API more consistent. Also - the textStatus (provided by jQuery) has been replaced with the HTTP response code.

Old signature:


success: function(data, textStatus, jqXHR)
complete: function(data, textStatus, jqXHR)
error: function(jqXHR, textStatus, errorThrown)

New signature:


success: function(data, responseCode, xhr)
complete: function(data, responseCode, xhr)
error: function(data, responseCode, xhr)

The method signatures are now consistent across the board.

9 Likes

Hello

I just saw in the v3.0.45 release that:
Moves AJAX framework from jQuery to Vanilla JS

I checked out the topic:
Introducing the new AJAX Framework + Turbo Router

Does this now mean, that jQuery AJAX framework functions are deprecated?

To be concrete. Does this in v3.0.45 still work?

$.request('onSomething', { data: someVar });
1 Like

Hey @mathilde,

Yes, it still works if jQuery is found. The new syntax is:

// jQuery API
$.request('onSomething', { data: someVar });

// Vanilla JS API
oc.ajax('onSomething', { data: someVar });

And for this

// jQuery API
$('.some-element').request('onSomething', { data: someVar });

// Vanilla JS API
oc.request('.some-element', 'onSomething', { data: someVar });
1 Like

This is awesome! Congrats on such a great release @daftspunky!:fire:

4 Likes