Skip to content

Latest commit

 

History

History
69 lines (47 loc) · 1.84 KB

javascript-hooks.md

File metadata and controls

69 lines (47 loc) · 1.84 KB

Title: JavaScript Hooks Description: Extend repeaters and matrix fields; or, the REST HTTP response.


Getting Started

TypeRocket comes with two callbacks to help you extend the core JavaScript functionality: TypeRocket.httpCallbacks and TypeRocket.repeaterCallbacks.

HTTP

The HTTP callback lets you modify and respond to the REST API's JSON response.

TypeRocket.httpCallbacks.push(function(response) {
   // do something
});

Example: Replacing flash with Sweet Alert

For example, you can tell the response that it shouldn't use the default flash message but use Sweet Alert instead.

TypeRocket.httpCallbacks.push(function(response) {
    response.flash = false;

    if (response.valid == true) {
        type = 'success';
        title = 'Success!';
    } else {
        type = 'error';
        title = 'Error!'
    }

    swal(title, response.message, type);
});

If you are using the theme options plugin, you can see the core flash message when you save your changes.

Repeater and Matrix

The repeater callback is designed to allow you to do something when a repeater or matrix field is added. Although the callback has repeater in its name don't forget it applies to matrix fields as well.

$template is a jQuery object of the field group being added.

TypeRocket.repeaterCallbacks.push(function($template) {
    // do something
    $template.find('.my-field').hide();
});

JavaScript Helpers

TypeRocket provides a helper object located at window.trHelpers. If you need to access these helpers from the front-end be sure you have front-end mode enabled.

Site URL

let site_url = window.trHelpers.site_uri;

Nonce

let tr_nonce = window.trHelpers.nonce;