Show and Hide the Toast

API

NOTE
In this article, the Button widget is used to demonstrate how to show and hide the Toast. This choice is made for purely demonstrational purposes, and you can do the same operations using another widget following the same guidelines.

To show or hide the Toast programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Toast, respectively.

JavaScript
$(function() {
    $("#toastContainer").dxToast({
        message: "Connection problem",
        type: "error"
    });

    $("#buttonContainer").dxButton({
        text: "Show the Toast", 
        onClick: function () {
            $("#toastContainer").dxToast("show");
            // ---------- or ----------
            $("#toastContainer").dxToast("toggle", true);
        } 
    });
});

With AngularJS or Knockout, use a different technique. Bind the visible property of the Toast widget to a scope property (in AngularJS) or an observable variable (in Knockout). After that, change this scope property or observable variable, and the Toast will appear or disappear.

AngularJS

HTML
<div ng-controller="DemoController">
    <div dx-toast="{
        message: 'Connection problem',
        type: 'error',
        bindingOptions: {
            visible: 'isToastVisible'
        }
    }"></div>
    <div dx-button="{
        text: 'Show the Toast',
        onClick: showToast
    }"></div>
</div>
JavaScript
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.isToastVisible = false;
        $scope.showToast = function () {
            $scope.isToastVisible = true;
        }
    });

Knockout

HTML
<div data-bind="dxToast: {
    message: 'Connection problem',
    type: 'error',
    visible: isToastVisible
}"></div>
<div data-bind="dxButton: {
    text: 'Show the Toast',
    onClick: function (e) {
        e.model.isToastVisible(true);
    }
}"></div>
JavaScript
var viewModel = {
    isToastVisible: ko.observable(false)
};

ko.applyBindings(viewModel);

User Interaction

Because the Toast is supposed to notify a user when something happens, it cannot be invoked from the UI. However, it may be hidden from the UI in many different ways. To decide which of them are available to the user, specify the following options.

Option Description
closeOnClick Hides the Toast when a user clicks/presses it.
closeOnOutsideClick Hides the Toast when a user clicks/presses outside of it.
closeOnSwipe Hides the Toast when a user swipes it out of the screen.
closeOnBackButton Hides the Toast when a user presses the Back button on the device.

Events

To execute certain commands before or after the Toast was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the widget, assign it to the corresponding onEventName option. For example, in the following code, a handler of the hidden event is assigned to the onHidden option. This handler counts down from three replacing the message in the Toast at the same time.

JavaScript
$(function() {
    var counter = 3;

    $("#toastContainer").dxToast({
        message: counter,
        displayTime: 500,
        onHidden: function (e) {
            counter--;
            if (counter != 0) {
                e.component.option("message", counter);
            } else {
                e.component.option({
                    message: "Go!",
                    onHidden: undefined,
                    displayTime: 3000,
                    type: "success"
                });
            }
            e.component.show();
        }
    });

    $("#buttonContainer").dxButton({
        text: "Start the Countdown", 
        onClick: function () {
            $("#toastContainer").dxToast("show");
        } 
    });
});

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method.

JavaScript
var hiddenEventHandler1 = function (e) {
    // First handler of the "hidden" event
};

var hiddenEventHandler2 = function (e) {
    // Second handler of the "hidden" event
};

$("#toastContainer").dxToast("instance")
    .on('hidden', hiddenEventHandler1)
    .on('hidden', hiddenEventHandler2);
See Also