Working with messaging

The communication between modules and plugins in Redactor built on the base of broadcasting and listen to messages. It makes the modules and plugins independent of each other.

This system reveals an incredible opportunity to create plugins. You can listen and connect to almost any function of Redactor.

How to listen messages in a plugin #

Let's see how to listen to messages in the plugin. Specify the name of the message you want to listen to and put on in the beginning. For example, you want to call some code in your plugin each time keydown event occurs in Redactor.

(function($R)
{
    $R.add('plugin', 'myplugin', {
        init: function(app)
        {
            // define app
            this.app = app;
        },

        // messages
        onkeydown: function()
        {
            // ... doSomething ...
        }
    });
})(Redactor);

It is easy, isn't?

Redactor has the names of the messages are compound, for example, source.open - the message triggered when the source code is opened. Here's how to listen to an event with a compound name:

(function($R)
{
    $R.add('plugin', 'myplugin', {
        init: function(app)
        {
            // define app
            this.app = app;
        },

        // messages
        onsource: {
            open: function()
            {
                // ... doSomething ...
            }
        }
    });
})(Redactor);

Look at the full list of messages.

How to broadcast the message from a plugin to another one #

Look at how to broadcast the message from one plugin to another. To broadcast a message from the plugin, you need to use the broadcast method.

(function($R)
{
    $R.add('plugin', 'myplugin', {
        init: function(app)
        {
            // define app
            this.app = app;
        },
        start: {
            this.app.broadcast('myplugin.mymessage');
            // with arguments
            this.app.broadcast('myplugin.myevent', 'arg1', 'arg2');
        }
    });
})(Redactor);

And then listen to the messages in another plugin.

(function($R)
{
    $R.add('plugin', 'anotherplugin', {
        init: function(app)
        {
            // define app
            this.app = app;
        },

        // messages
        onmyplugin: {
            mymessage: function()
            {
                // ... doSomething ...
            },
            myevent: function(arg1, arg2)
            {
                // ... doSomething ...
            }
        }
    });
})(Redactor);

How to listen messages from a plugin outside #

Listen the message with callbacks option.

$R('#redactor', {
    callbacks: {
        myplugin: {
            started: function()
            {
                // ... do something ...
            }
        }
    }
});

Broadcast the message from a plugin:

(function($R)
{
    $R.add('plugin', 'myplugin', {
        init: function(app)
        {
            // define app
            this.app = app;
        },
        start: {
            this.app.broadcast('myplugin.started');
        }
    });
})(Redactor);