Data sharing and data delivery complement each other, let’s discuss this issue together. The first thing to say is that sharing and passing are both scoped. Scope is the area where it works. Data can be shared in the same scope. If you exceed this scope, it is cross-scope, and data transmission must be used.
Scope
1.ui scope
Each ui file has a corresponding ui.js by default. They serve as a closed scope. ui.js obtains ui objects based on the id of the component in the ui file; different ui files can define components with the same id. Variables defined in ui.js can only be accessed in this js.
2.page scope
Every time you call openPage, a new page will be opened, and this new page will be covered on the old page. When closePage closes itself, the covered old page will be exposed. In addition to the main ui file, each page can also contain many other ui files, which are in the same page scope.
When the page is closed, all objects built in the page will be released.
3.app scope
This is the largest scope. As long as the app does not exit, this scope will always be valid.
app.js belongs to the app scope because it does not belong to any page.
In short, the app scope contains multiple page scopes, and the page scope contains multiple ui scopes.
Memory sharing
Compared with files and databases, memory operations are much faster and are suitable for relatively small data operations. The disadvantage is that the app is released after it is closed. deviceone shares memory in the following ways.
1. Do_Global's memory operation (app scope)
This is data sharing in the app scope. This piece of memory is actually a key-value pair, and a key corresponds to a value, so be careful that if a key is reassigned, the previous value will be overwritten. The method of using is very simple. Refer to the following examples, read and write in different pages respectively.
// Set the value in index.ui.js, which can be set to any json object, with function object exceptions. global.setMemory("key1", 1);global.setMemory("key2", "value1");global.setMemory("key3", [ "a", "b", "c" ]);global.setMemory("key4", {"k1" : "v1","k2" : "v2","k3" : "v3","k4" : "v4"}); var label = ui("do_Label_2");// Get the value in memory/index.ui.js, you can directly return the json object var global = sm("do_Global");var content = {};content.key1 = global.getMemory("key1");content.key2 = global.getMemory("key2");content.key3_2 = global.getMemory("key3")[1];content.key4_k3 = global.getMemory("key4")["k3"];label.text = JSON.stringify(content, null, 2);// Format2. Javascript global variables (page scope)
Use JavaScript's own characteristics to define global variables. You can usually define global variables to realize data sharing in different ui files under the same page. Referring to the following example, read and write in different ui files, but are in a page scope. It is also very simple to use, there are two ways to:
Although it is very convenient, it is not recommended to use it because it is too casual. If it is a collaborative development or a complex project, it is difficult to locate and debug if there is a bug.
// Set the global variables of js in test1.ui.js, two ways. // 1. Do not add var prefix variable definition, key1 = "value1";// 2. Define global variables on deviceone object deviceone.key2 = {"k1" : "v1","k2" : "v2","k3" : "v3","k4" : "v4"} // Get the global variables defined in test1.ui.js in test2.ui.js, two ways. var content = {};content.key1 = key1;content.key2_k3 = deviceone.key2["k3"];3. Javascript variables (ui scope)
This does not need to be explained too much, but is the normal js variable definition, which can only be valid in the current ui.js scope.
var key1 = "value1";
4. Memory mode of sqlite
SQLite is usually a file mode. There is a special situation where SQLite can be used directly in memory. It is suitable for a way to use complex data structures and troublesome text operations. Using SQL statements will be much more flexible.
There can only be one memory mode, and the name is fixed as /:memory/:.
In the following description of the SQLite database, we will introduce it in detail.
File Sharing
This is easy to understand by everyone. File sharing is in the scope of the app and can also be accessed after the app is restarted. You can write content to a file anywhere in the app through the do_Storage component, and then read a file from another place to read the content. Refer to the following examples, read and write in different pages respectively. What you need to note here is that file reading and writing are usually asynchronous, and you have to make sure that the content has been written before you can read it.
// Write files file1 and file2 in index.ui.js, you can directly write json objects var key1 = "value1";storage.writeFile("data://file1", key1, function(data, e) {// The callback only ends here to write the content. If you read the file before executing here, it may not be able to read the data}) var key2 = {"k1" : "v1","k2" : "v2","k3" : "v3","k4" : "v4"};storage.writeFile("data://file2", key2, function(data, e) {// The callback only ends here to write the content. If you read the file before executing here, it may not be able to read the data}) // Get the value in datacache/index.ui.js, you can directly return the json object var datacache = sm("do_DataCache");var content = {};content.key1 = datacache.loadData("key1");content.key2_3 = datacache.loadData("key2")["k3"];label.text = "datacache/index.ui.js, you can directly return the json object /n"+ JSON.stringify(content, null, 2);// Formatdo_SQLite component accesses database data
This component is an MM component, meaning that multiple instances can be created. All MM components are page scope by default, or app scope. Create the third parameter marking scope of the MM component.
It should be noted here that SQLite reading and writing are usually asynchronous, you have to make sure that the content has been written before you can read it.
1. App scope:
// Create an app-scope sqlite object. The second parameter is the label of this object. The third parameter marks the scope is appvar sqlite_app = mm("do_SQLite", "sqlite_app_id1", "app")function test_sqlite() {// Use this object to create a database test.dbsqlite_app.open("data://test.db");var stu_table = "drop table if exists stu_table"// Execute a SQL statement sqlite_app.executeSync(stu_table);// Create table SQL statement stu_table = "create table stu_table(_id integer);// Create table SQL statement stu_table = "create table stu_table(_id integer); primary key autoincrement,sname text,snumber text)";// Synchronously execute a SQL statement sqlite_app.executeSync(stu_table);var stu_sql = "insert into stu_table(sname,snumber) values('xiaoming','01005');"+ "insert into stu_table(sname,snumber) values('xiaoliu','01007')";// Execute a SQL statement asynchronously sqlite_app.execute(stu_sql, function(data, e) {// The callback is only here that it is really inserted into the data. If you query the data before executing it, it may not be able to read the data deviceone.print("insert finished!")}) // Obtain an app-scope sqlite object based on the id "sqlite_app_id1" and the second parameter is the label of this object. The third parameter indicates that the scope is appvar sqlite_app = mm("do_SQLite", "sqlite_app_id1", "app")// Use this object to query test.db in sqlite/index.ui.js, because this object has opened the database, there is no need to open it anymore// Create query SQL statement var stu_query = "select * from stu_table";// Execute a query statement synchronously var result = sqlite_app.querySync(stu_query);label.text = "Use this object in sqlite/index.ui.js to query the second piece of data of the stu_table table in test.db/n"+ JSON.stringify(result[1], null, 2);2. page scope:
// Create a page-scoped SQLite object, the only id label is memory_db_id1var sqlite_app = mm("do_SQLite", "memory_db_id1", "page");// Use this object to create a memory database in test1.ui.js. This name must be written as: memory:sqlite_app.open(":memory:");// Create table SQL statement var stu_table = "drop table if exists stu_table;"// The memory database execution speed is fast, you can try to use synchronization // Execute a SQL statement sqlite_app.executeSync(stu_table);stu_table = "create table stu_table(_id integer primary key autoincrement,sname text,snumber text)";// Synchronously execute a SQL statement sqlite_app.executeSync(stu_table);var stu_sql = "insert into stu_table(sname,snumber) values('laoming','1');"+ "insert into stu_table(sname,snumber) values('laohong','2');"+ "insert into stu_table(sname,snumber) values('laoliu','3')";// Synchronously execute a SQL statement sqlite_app.executeSync(stu_sql); // Query the database table created in test1.ui.js in test2.ui.js// Obtain the created sqlite object var sqlite_app = mm("do_SQLite", "memory_db_id1", "page"); // Create query SQL statement var stu_query = "select * from stu_table";// Execute a query statement var result = sqlite_app.querySync(stu_query);label.text = "Query the third record of the in-memory database table created in test1.ui.js in test2.ui.js/n"+ JSON.stringify(result[2], null, 2)Data delivery
Data delivery involves cross-scope, such as passing data in different ui files and passing data in different pages.
The most important and most commonly used method is the message mechanism
1. Message mechanism
We will introduce this link in detail in the document.
In short, the message mechanism can pass data across ui scopes or across page scopes.
2. OpenPage and closePage pass data.
This data transfer is cross-page scope, but is limited to two-layer pages. For example, if you open page2 on page1, page1 passes some data to page2; page2 closes itself, exposes page1, and passes data back to page1. Data transfer can be any json object.
This is a regular and very good way, and it is recommended to use it like this.
// In index.ui.js, pass data var d = {"k1" : "v1","k2" : "v2","k3" : "v3","k4" : "v4"};app.openPage({source : "source://view/open_close_page/index.ui",data : d,statusBarState : "transparent"});}// Accept the data passed back when the page open_close_page/index.ui is closed. page.on("result", function(data) {if (data)nf.alert(JSON.stringify(data, null, 2));}) // The data passed from index.ui.js can get the value through getData, and can directly return the json object var data = page.getData();label.text = "The data passed from index.ui.js can get the value through getData, and can directly return the json object/n"+ JSON.stringify(data, null, 2);// Format function close_me() {// Close itself and pass the data back to the next layer of pageapp.closePage("I passed the data when open_close_page/index.ui is closed");}I will introduce so much about the relevant knowledge about js data sharing and data transmission that this article introduces to you. I hope it will be helpful to you!