The message/event mechanism is a mechanism that almost all development languages have. It is not the originality of deviceone. It is called message (Event) in some languages, and Message in some places. In fact, the principle is similar, but some implementation methods are a little more complicated. Our deviceone is called message.
Message basic concept
There are also some beginners who are not very familiar with this mechanism. Let’s briefly introduce some basic concepts. If you are familiar with it, you can skip this part.
A/message can be understood as a data structure, including the following basic parts:
1. Message source: It is the source of the message, the object that sent the message
2. Message name: It is the only label of the message
3. Message data: The data attached after the message is sent, and the data may be empty.
There are two types of messages:
1. System message: The message sent by the operating system or deviceone system has a fixed name.
2. Custom messages: The developer defines the message itself. The message sent by himself is arbitrary and can be defined arbitrarily.
Give an example:
For example, if a user clicks a do_Button button, a system message will be triggered, including 3 parts:
1. Source: button object in user point
2. Message name: touch
3. Message data: This message has no data attached
For example, the user triggers a custom event through the do_Button button, which contains 3 parts:
1. Source: button object
2. Message name: User can define it casually, namely aaa, bbb, or ccc
3. Message data: The accompanying data is set when the message is triggered
Publish/Subscribe Mode
The publish/subscribe mode is one of the most commonly used design modes and is the core of the message mechanism. Its characteristic is to reduce the coupling degree and prevent the two independent objects from dependent on each other. Let me give you a brief introduction, familiar students can skip it.
Let’s first illustrate this problem from a simple example in reality, refer to the figure below:
From this picture we can see
1. Consumers and publishers do not know each other, and consumers do not need to know which publisher they want to publish the magazine; publishers do not need to know which person has booked the book they publish.
2. Both consumers and publishers must know the post office.
3. Consumers need to tell the post office the name and address of the consumer and the magazine name you want to subscribe to
4. Multiple consumers can subscribe to the same magazine
5. After the post office receives the magazine, it will notify the consumer one by one, and when the notice is made, the magazine will be delivered to the consumer at the same time.
After looking at the above realistic example, let’s look at the abstract description and make it clearer. Look at the following picture:
Corresponds to the above actual example description:
1. The system/developer and function objects do not depend on each other. The system/developer only triggers a message and does not care about who will accept it.
2. System/developer and function objects must be able to obtain the message source object
3. When subscribing to a message, the name of the message and the reference of the function object must be marked.
4. Multiple function objects can subscribe to messages with the same name in the same message source
5. The message source triggers the message to all subscribers one by one and passes the data data to the callback function object.
After reading the abstract description, let’s finally look at the actual deviceone development example, or take do_Button as an example.
1. When the user clicks on a button and touches it, the system will obtain the button object as the message source, and fire a "touch" message. Any function object subscribed to the "touch" message will receive this message and cause the function to be executed.
//Get button object var btn_hello = ui("btn_hello");//Define function object function f(){//When btn_hello button receives a finger click, the following code will be executed deviceone.print("f function receives a click trigger message")}function f(){//When btn_hello button receives a finger click, the following code will be executed deviceone.print("f function receives a click trigger message")}//f, f subscribe to the button's touch message btn_hello.on("touch",f); btn_hello.on("touch",f);2. We can define 2 custom messages "message1" and "message2" for the button object, and there are 2 function objects to subscribe to these 2 messages. However, in the end, the developer must call the fire function to trigger this message. This is the difference between it and system messages.
//Get button object var btn_hello = ui("btn_hello");//Define function object function f(d){//When btn_hello button receives the message triggered by the developer, the following code will be executed deviceone.print("f function receives the message message, and the message data is: "+d)}function f(d){//When btn_hello button receives the message triggered by the developer, the following code will be executed deviceone.print("f The function receives a message message, and the data of the message is: "+d)}//f, f subscribes to the button's touch message btn_hello.on("message",f); btn_hello.on("message",f);//triggers the message btn_hello.fire("message","data"); btn_hello.fire("message","data");Seeing this, you will definitely wonder why we want to customize objects on buttons? Is this meaningful? In fact, it is indeed meaningless and unnecessary. Here we are just taking buttons as an example. In conventional development, it is basically not used like this.
Use of messages
I have said so much before, and now it is the use of deviceone messages. It is actually very simple to use. The above example basically illustrates how to use system events and custom events.
Let me explain a few concepts
1. All objects of deviceone, including UI, MM, and SM objects, can be information sources
// The SM object can be the message source var page = sm("do_Page");page.on("loaded",function()){// This is the system message of the page object. This message does not need to be triggered manually, the system will automatically trigger }page.on("message",function(d)){// This is the custom message of the page object}page.fire("message","data");// The MM object can be the message source var http = mm("do_Http"); http.on("result",function()){// This is the system message of the http object. This message does not need to be triggered manually, and will automatically trigger after receiving feedback from the http server}http.on("message",function(d)){// This is a custom message for the http object}http.fire("message","data");//The UI object can be the message source var alayout = ui("alayout_id");alayout.on("touch",function()){// This is a system message for the alayout object. This message does not need to be triggered manually, and it will be triggered by the mobile phone clicking}alayout.on("message",function(d)){// This is a custom message for the alayout object}alayout.fire("message","data");2. The message source object is scoped, so the subscribed and triggered message source must be the same object with the same scope. Here we understand it in combination with data sharing and data delivery documents.
Looking at the following example, test1.ui and test2.ui may be in a page scope, or may not be in a job scope. Only messages in a fire can be delivered correctly.
To determine whether it is the same, you can print the page address page.getAddress().
//Subscribe to the message in test.ui.js var page = sm("do_Page");deviceone.print(page.getAddress());page.on("message",function(d)){deviceone.print(d);}//Trigger the message in test.ui.js var page = sm("do_Page");deviceone.print(page.getAddress());page.fire("message","data");If you are not in the same page scope, you can subscribe to the app scope that can be shared with both pages
Change the above code to:
//Subscribe to the message in test.ui.js var app = sm("do_App");app.on("message",function(d)){deviceone.print(d);}//Trigger the message in test.ui.js var app = sm("do_App");app.fire("message","data");3. The same function object can repeatedly subscribe to a message from an object source. When the message is triggered, the function will be executed multiple times. This is a common mistake for beginners.
var page = sm("do_Page");var count = ;function f(){deviceone.print("number of executions"+(count++));}page.on("message",f);page.on("message",f);page.fire("message");Looking at the example above, if executed, it will print 2 this, because you have subscribed twice, maybe you would say who would write such code? The actual situation is definitely not so easy to see that the repeated on function is executed. The actual situation is often like executing the on function in a click event, and the subscription is repeated once every time the button is clicked.
4. The subscription of messages must be before the message is triggered, which is a common mistake for beginners.
var page = sm("do_Page");var count = ;function f(){deviceone.print("number of executions"+(count++));}page.fire("message");page.on("message",f);Looking at the example above, if executed, it will have no effect. Maybe you would say who would write such code? The actual situation is definitely not so easy to see that the order is reversed. The actual situation is often like the on function is executed in the callback function of a certain function. You cannot determine when the callback function is executed and whether it is executed before fire. Generally, when encountering this situation, you can add several devices.print to print it to see if it is executed first or fire first.
5. If you have a subscription, you will unsubscribe. Unsubscribe is an off function. The reason why it is rarely used is because when you closePage, all the messages subscribed to the current page scope will be automatically released.
However, if the message subscription is in the scope of the app, you should be careful that you may need to manually unsubscribe. Otherwise, the function will be executed multiple times when the message is triggered.
var page = sm("do_Page");var count = ;function f(){deviceone.print("number of executions"+(count++));}page.on("message",f);page.fire("message");.page.off("message");page.fire("message");Looking at the example above, printing will only be performed once, because the fire will be unsubscribed after one time.