(Quick Reference)

on([String namespace ,] String topic, Closure callback)

Available in Controllers, Domains and Services

This method dynamically registers a closure handler for a given namespace/topic. Default Namespace is app if undefined. It returns a stringified ListenerId.

Usage

class SomeController{

 def testInlineListener = {
        //register with 'logout' topic on 'app' default namespace
        def listener = on("logout") {User user ->
            println "test $user"
        }
        render "$listener registered"
 }

 def testInlineListener2 = {
        //register a 'gorm' namespaced handler on 'afterInsert' topic.
        def listener = on("gorm", "afterInsert") {Book book ->
            println "test $book"
        }
        render "$listener registered"
 }
}

Arguments

NameMandatoryDefaultDescription
namespacefalse app A string scope
topictrue A string topic to listen for
callbacktrue A closure handler which takes an EventReply as an argument