Publish and subscribe to messages
The essence of Ext.net Messagebus is a message subscription mechanism, and there is no such mechanism in ExtJS, So Messagebus's ext.net, but not original, has been implemented in dojo for a long time and can be found in my blog: Getting Started with Dojo: event handling in Dojo
The following code shows how to use Messagebus to publish and subscribe to a message:
<ext:runatIDTextHandler= "Fnbuttonhandler" ></ext:button >
First, a button is defined, and when clicked, the Fnbuttonhandler method is executed, and we publish the message in this method:
Fnbuttonhandler () { Ext.net.Bus.publish (
"Custom.event1", {item: this
});}
The first parameter of the Ext.net.Bus.publish method is the message name, and the second parameter is the parameter of the message.
Next we subscribe to this message:
Ext.onready (() { Ext.net.Bus.subscribe (
"custom.*", function
() { console.log (arguments); });
In the Onready event, add a subscription to the message custom.*, where a wildcard character is used to represent all messages that start with the custom.
After the code is finished, we refresh the page and then click the button, and this time the console is displayed as follows:
This is a message publication and subscription that we implement through code, and Ext.net also allows you to publish and subscribe to messages via controls.
Publishing and subscribing to messages via controls
I'm going to demonstrate here through a ready-made example:
<Ext:PanelId= "Panel1"Runat= "Server"Title= "Messagebus Event Logger"Width= "400"Height= "300"AutoScroll= "true" > <Messagebuslisteners> <Ext:MessagebuslistenerName= "app.*"Handler= "This.body.createChild ({html: ' Received event from ' + data.item.id, tag: ' P '});"/> </Messagebuslisteners> <Buttons> <Ext:ButtonId= "Button1" runat= "text= "Publish Event 1" > <listeners> <broadcastonbus = "App.event1"/> </listeners> </ext:button> </buttons></ext :panel>
In this code, we publish the message via the button's click event, and the Control Panel subscribes to the message with the following effect:
Server-side message publishing and subscription processing
The above demo is done on the client, the next one is very powerful, on the server side of the message publishing and subscription, see the code:
<Ext:ButtonRunat= "Server"Id= "Btnpublish"Text= "Message publisher"Ondirectclick= "Btnpublish_directclick" ></Ext:Button><Ext:ButtonRunat= "Server"Id= "Btnsubscribe" text= "Message subscribers" > <messagebusdirectevents> <ext:messagebusdirectevent Name= "app.*" onevent= "subscribe _event "> </ext: Messagebusdirectevent> </MessageBusDirectEvents< Span style= "color:blue;" >></ext:Button>
The first is two buttons, one for publishing messages and one for subscribing to messages.
In the Message Publisher button, we used the Ondirectclick event, which is a server-side processing event that is shorthand for the direct click event.
In the Subscriber button, we add the relevant configuration of the subscription and process the subscribed message through the event handling method subscribe_event, the corresponding code on the server side is as follows:
Btnpublish_directclick (sender, ext.net. Ext.net.
Messagebus. Default.publish ("App.server"
);} subscribe_event (sender, ext.net. X. Messagebox.alert ("hint"+ e.name). Show ();}
Recompile the code, and then refresh our page. When you click the Message Publishing button, we receive a server-side message:
Have a friend think about the execution of this code.
1. Trigger the server-side button click, post the message in the button click event, and publish the message on the client
2. The Client Subscriber button receives the publication of the message
3. The Subscriber button sends the message to the server side for processing
Summarize
Ext.net Messagebus can easily complete the message publishing and subscription mechanism, and can be used in both foreground and background code.
Ext.net Learning notes 03:ext.net messagebus usage