A deep analysis of _javascript techniques of data sharing and data transmission in JavaScript

Source: Internet
Author: User
Tags sqlite unique id

Data sharing and data transfer are mutually reinforcing and we discuss this issue together. The first thing to say is that sharing and passing are scoped. Scopes are areas of work in which the same scope data can be shared, over which the scope is cross scoped, and data transmission is used.

Scope

1.ui Scope

Each UI file defaults to a corresponding ui.js. them as a closed scope. Ui.js gets the UI object based on the ID of the component in the UI file, and 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

Each time the call Openpage opens a new page, the new page is built over the old page, and Closepage closes itself to reveal the old page that was covered. Each page, in addition to the main UI file, can contain a number of other UI files that are in the same page scope.
When the page closes, all objects built in the page are released.

3.app Scope

This is the largest scope, and as long as the app does not exit, the scope is always valid.

App.js belongs to the app scope because it does not belong to any page.

In summary, the app scope contains more than one page scope, and the page scope contains multiple UI scopes.

Memory sharing

Compared to files and databases, memory operations are much faster and are suitable for operations with less data volume. The downside is that app shuts down and releases it. Deviceone share memory in several ways.

1. Do_global Memory operation (app scope)

This is the app scope data sharing. This memory is actually a key value pair, a key corresponds to a value, so note that if you assign a key to a value, you will overwrite the previous value. The use of the method is simple. Refer to the following example, read and write separately in different page.

Setting the value in Index.ui.js can be set to any JSON object, except for the function object.
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 the Memory/index.ui.js, you can return the JSON object
var global = SM ("Do_global") directly;
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"];

2. JavaScript Global Variables (page scope)

Using JavaScript's own features to define global variables, you can usually define global variables to implement data sharing in different UI files under the same page. Refer to the following example, read and write separately in different UI files, but in a page scope. The use is also very simple, there are two ways:

Although very convenient, but not recommended, because the use of too random, if it is collaborative development or complex projects, if encountered bugs, difficult to locate and debug.

Set JS global variable in test1.ui.js, two ways.
//1. Do not add var prefix variable definition,
key1 = "value1";
2. Define the global variable on the Deviceone object
deviceone.key2 = {"
K1": "V1",
"K2": "
v2"
, "K3": "V3", "K4": "V4"}
   //in Test2.ui.js to get the global variable defined in Test1.ui.js, two ways.
var content = {};
Content.key1 = Key1;

3. JavaScript variables (UI scope)

This is not too much to explain, is the normal JS variable definition can only be valid for the current ui.js scope.

 
 

4. SQLite Memory Mode

SQLite is usually a file pattern, there is a special situation can be used directly in memory to use SQLite, suitable for the data structure is more complex, the text operation cumbersome way, the use of SQL statement operations will be much more flexible.

Memory mode can only have one, the name is fixed to \:memory\:.

In the following SQLite the database introduction of the place in detail.

File sharing

This is well understood, file sharing is app scoped and can be accessed after the app restarts. You can write content to a file anywhere in the app by using the Do_storage component, and then read the content in a different place by reading a file. Refer to the following example, read and write separately in different page. Note that the file reads and writes are usually asynchronous, and you have to make sure that the content is finished before you can read it.

Write file file1 and file2 in Index.ui.js, you can write JSON object
var key1 = "value1" directly;
Storage.writefile ("Data://file1", Key1, function (data, e) {
//callback is here to actually write the content, if you read the file before you execute it may not read the data
})
var key2 = {
"k1": "V1",
"K2": "V2",
"K3": "V3",
"K4": "V4"
};
Storage.writefile ("Data://file2", Key2, function (data, e) {
//callback is here to actually write the content, if you read the file before you execute it may not read the data
}) 
//In datacache/index.ui.js to get the value, you can return the JSON object directly
var datacache = SM ("Do_datacache");
var content = {};
Content.key1 = Datacache.loaddata ("Key1");
Content.key2_3 = Datacache.loaddata ("Key2") ["K3"];
Label.text = "Fetch value in Datacache/index.ui.js, return directly to JSON object \ n"

Do_sqlite component access to database data

This component is a mm component, meaning that multiple instances can be created. All mm components are either by default page scope or app scope. Create a mm component The third parameter marks the scope.

The note here is that SQLite reads and writes are usually asynchronous, and you have to make sure that the content is finished before you can read it.

1. App Scope:

Create a SQLite object for the app scope, the second parameter is the object's identifier, and the third argument is that the scope is app var Sqlite_app = mm ("Do_sqlite", "Sqlite_app_id1", "app") function Test_sqlite () {//Index.ui.js the object to create a database test.db Sqlite_app.open ("data://test.db"); var stu_table = "DROP table if ex
ists stu_table "//synchronously executes an SQL statement sqlite_app.executesync (stu_table);
CREATE TABLE SQL statement stu_table = "CREATE Table stu_table (_id integer primary key autoincrement,sname text)";
Executes a SQL statement Sqlite_app.executesync (stu_table) synchronously; var stu_sql = "INSERT into stu_table (sname,snumber) VALUES (' Xiaoming ', ' 01005 ');" + "inserts into Stu_table (Sname,snumber)
VALUES (' Xiaohong ', ' 01006 '); "+" INSERT into stu_table (sname,snumber) VALUES (' Xiaoliu ', ' 01007 ');
Asynchronously executes an SQL statement sqlite_app.execute (stu_sql, function (data, E) {//callback here to actually insert the data, and if you query the data before you execute it, you might not be able to read it. 
Deviceone.print ("Insert Finished!")}) Get an app scoped SQLite object based on the id "SQLITE_APP_ID1", the second parameter is the object's identifier, and the third argument is the app var sqlite_app = mm ("Do_sqlite", "Sqlite_ App_id1 ", App")//Use this object in Sqlite/index.ui.js to query test. db, because this object already has a database open, so don't need to open//Create a 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 data of the stu_table table in test.db \ n" + json.stringify (result[1], NULL, 2);

2. Page scope:

Creates a SQLite object with a page scope, with the unique ID labeled memory_db_id1 var sqlite_app = mm ("Do_sqlite", "memory_db_id1", "page");
In test1.ui.js use this object to create a memory database, the name must be written dead: Memory:sqlite_app.open (": Memory:"); CREATE TABLE SQL statement var stu_table = "drop table if exists stu_table;"//Memory database executes quickly, and you can try to execute an SQL statement with synchronization//synchronization Sqlite_app.executesync
(stu_table);
stu_table = "CREATE Table stu_table (_id integer primary key autoincrement,sname text)";
Executes a SQL statement Sqlite_app.executesync (stu_table) synchronously; var stu_sql = "INSERT into stu_table (sname,snumber) VALUES (' laoming ', ' 1 ');" + "inserts into Stu_table (sname,snumber) value
S (' Laohong ', ' 2 '); "+" INSERT into stu_table (sname,snumber) VALUES (' Laoliu ', ' 3 '); 
Executes a SQL statement Sqlite_app.executesync (stu_sql) synchronously; Query in the Test2.ui.js database table created in Test1.ui.js//According to MEMORY_DB_ID1 this indicator to get the SQLite object that has been created 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 synchronously var result = Sqlite_app.querysync (stu_query); Label.text = "in Test2.ui. JS Query the third record of the Memory database table created in Test1.ui.js \ n "+ json.stringify (result[2], NULL, 2)  

Data delivery

Data delivery involves cross scope, such as different UI file delivery data, different page pass data.

The most important and most common way is the message mechanism

1. Message mechanism

We'll explain this in more detail in the documentation.

In summary, the messaging mechanism can pass data across UI scopes or across page scopes.

2.openPage and Closepage pass data.

This data transfer is across the page scope, but is limited to between two layers of page. For example, on the basis of Page1 open page2,page1 some data to page2;page2 close itself, exposing Page1, and can pass data back to Page1. Data passing can be any JSON object.
This is a regular and very good way to use it.

In Index.ui.js openpage page open_close_page/index.ui, passing 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
page.on ("Result", function (data) {
if (data)
Nf.alert (Open_close_page/index.ui) that is passed back when the page is closed Json.stringify (data, NULL, 2);
}) 
The data passed from Index.ui.js is obtained by GetData, which returns the JSON object
var data = Page.getdata () directly.
Label.text = "The data passed from the index.ui.js is obtained by GetData and can be returned directly to the JSON object \ n"
+ json.stringify (data, NULL, 2);//Format
function Close_me () {
//closes itself, passing data back to the next layer of page
app.closepage ("I passed the data from the Open_close_page/index.ui close");
}

About this article for you to introduce the JS data sharing and data transmission related knowledge to everyone to introduce so much, I hope to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.