Data sharing and data transfer

Source: Internet
Author: User

Data sharing and data transfer are mutually reinforcing, and we discuss this issue together. The first thing to say is that both sharing and passing are scoped. Scopes are areas of work that can be shared at the same scope, where data is passed when more than one scope is cross-scoped.

Scope
    1. UI Scopes
      Each UI file has a corresponding ui.js by default. They act as a closed scope. Ui.js gets the UI object based on the ID of the component in the UI file; Different UI files can define components of the same ID. The variables defined in the ui.js can only be accessed in this JS.

    2. Page scope
      Each call to Openpage will open a new page, the new page will be covered on the old page, Closepage close himself will reveal the old page covered. In addition to the main UI file, each page can contain many other UI files, which are in the same page scope.
      When the page is closed, all objects built in the page are freed.

    3. App Scopes
      This is the largest scope and the scope is valid as long as the app does not exit.

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

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

Memory sharing

Relative to files and databases, memory operations are much faster and are suitable for operations with less data volume. The downside is that the app is released after it's closed. The Deviceone shares memory in several ways.

1. Do_global Memory Operation ( App Scopes)

This is the app-scoped data sharing. This piece of memory is actually a key value pair, a key corresponds to a value, so be aware that if a key is re-assigned, it will overwrite the previous value. The method of use is simple. Refer to the following example, read and write separately on different page.

// set the value in Index.ui.js, which 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 Memory/index.ui.js and return to the JSON object directly var global = SM ("Do_global"); var content == global.getmemory ("Key1"= global.getmemory ("Key2"= Global.getmemory ("Key3") [1= global.getmemory ("Key4") ["K3"null, 2);   Formatting
2. JavaScript Global Variables ( Page Scope)

Using JavaScript's own attributes to define global variables, it is often possible to define global variables for sharing data 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 it is convenient, it is not recommended because it is too casual to use, if it is a collaborative development or complex project, if you encounter a bug, it is difficult to locate and debug.

// set the global variable of JS in Test1.ui.js, two kinds of ways.  //  1. Do not add the variable definition of var prefix,key1 = "value1"; // 2. Define the global variable on the Deviceone object Deviceone.key2 = {    "K1": "V1",    "K2": "V2",    /c12> "K3": "V3",    "K4": "V4"}
// in Test2.ui.js, we get the global variables defined in Test1.ui.js in two ways.  var content == = deviceone.key2["K3"];
3. JavaScript variables ( UI Scopes)

This does not have too much explanation, is the normal JS variable definition, only in the current ui.js scope valid.

var key1 = "value1";
4. SQLite's Memory mode

SQLite is usually a file mode, there is a special situation can be directly in memory using SQLite, 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\: .
This is described in more detail in the following SQLite database.

File sharing

This is well understood, file sharing is the app scope, and it can be accessed after the app restarts. You can use the Do_storage component to write content to a file anywhere in the app, and then read a file in another place to read it. Refer to the following example, read and write separately on different page. It is important to note that file reads and writes are usually asynchronous, and you have to make sure that the content is finished before you can read it.

//write files File1 and file2 in Index.ui.js, you can write JSON objects directly    varKey1 = "Value1"; Storage.writefile ("Data://file1", Key1,function(data, e) {//the callback is here to actually finish the content, and if you read the file before you do it, you may not be able to read the data .    })    varKey2 = {        "K1": "V1",        "K2": "V2",        "K3": "V3",        "K4": "V4"    }; Storage.writefile ("Data://file2", Key2,function(data, e) {//the callback is here to actually finish the content, and if you read the file before you do it, you may not be able to read the data .})
// get the value in Datacache/index.ui.js and return to the JSON object directly var datacache = SM ("Do_datacache"); var content == Datacache.loaddata ("Key1"= Datacache.loaddata ("Key2") ["K3"= " Datacache/index.ui.js gets the value that can be returned directly to the JSON object \ n "        null, 2); // Formatting
Do_sqlite component access to database data

This component is a mm component, which means that multiple instances can be created. All mm components are either page scopes or app scopes by default. Create a third parameter indicating the scope of the MM component.

It is important to note that SQLite reads and writes are usually asynchronous, and you have to make sure that the content is finished before you can read

1. App Scope:
//Create an app-scoped SQLite object, the second parameter is the object's label, and the third parameter indicates the scope of the appvarSqlite_app = mm ("Do_sqlite", "Sqlite_app_id1", "app")functionTest_sqlite () {//use this object in Index.ui.js to create a database test.dbSqlite_app.open ("Data://test.db"); varstu_table = "DROP table if exists stu_table"//executing an SQL statement synchronouslySqlite_app.executesync (stu_table); //To create a table SQL statementstu_table = "CREATE Table stu_table (_id integer PRIMARY key autoincrement,sname text,snumber text)"; //executing an SQL statement synchronouslySqlite_app.executesync (stu_table); varStu_sql = "INSERT into stu_table (sname,snumber) VALUES (' Xiaoming ', ' 01005 ');"            + "INSERT into stu_table (sname,snumber) VALUES (' Xiaohong ', ' 01006 ');" + "INSERT into stu_table (sname,snumber) VALUES (' Xiaoliu ', ' 01007 ')"; //executes an SQL statement asynchronouslySqlite_app.execute (Stu_sql,function(data, e) {//the callback is here to actually insert the data, if it is possible to read the data before executing hereDeviceone.print ("Insert finished!")    })
 //  according to "SQLITE_APP_ID1" This ID gets an app-scoped SQLite object, the second parameter is the object's label, the third parameter indicates the app  var  Sqlite_app = MM ("Do_sqlite", "Sqlite_app_id1", "app"  //   Span style= "color: #008000;" >//  create query SQL statement  var  stu_query = " SELECT * from stu_table ";  //  synchronous execution of a query statement  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 page-scoped SQLite object with a unique ID indicating memory_db_id1varSqlite_app = mm ("Do_sqlite", "memory_db_id1", "page");//using this object in Test1.ui.js to create a memory database, the name must be written dead: Memory:Sqlite_app.open (": Memory:");//To create a table SQL statementvarstu_table = "DROP table if exists stu_table;"//Memory database execution is fast and can be attempted with synchronous//executing an SQL statement synchronouslySqlite_app.executesync (stu_table); Stu_table= "CREATE table stu_table (_id integer PRIMARY key autoincrement,sname text,snumber text)";//executing an SQL statement synchronouslySqlite_app.executesync (stu_table);varStu_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 ')";//executing an SQL statement synchronouslySqlite_app.executesync (Stu_sql);
// querying database tables created in Test1.ui.js in Test2.ui.js // get the SQLite object that you've created, based on the MEMORY_DB_ID1 flag. var sqlite_app = mm ("Do_sqlite", "memory_db_id1", "page"); // To create a query SQL statement var stu_query = "SELECT * from stu_table"; // executing a query statement synchronously var result == "Query in Test2.ui.js for the third record of the Memory database table created in Test1.ui.js \ n"        null, 2)
Data transfer

Data passing involves cross-scopes, such as different UI files that pass data, and different page-passing data.
The most important and most common way is the message mechanism

      1. Message mechanism
        This link is described in detail in the documentation.
        In summary, messaging mechanisms can pass data across the UI scope, or across page scopes.

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

//openpage page Open_close_page/index.ui in index.ui.js, passing data    varD = {        "K1": "V1",        "K2": "V2",        "K3": "V3",        "K4": "V4"    }; App.openpage ({source:"Source://view/open_close_page/index.ui", Data:d, Statusbarstate:"Transparent"    });}//accepts data that is passed back when the page Open_close_page/index.ui closedPage.On ("Result",function(data) {if(Data ) Nf.alert (json.stringify (data),NULL, 2));})
// the data passed from Index.ui.js gets the value through GetData and can be returned directly to the JSON object . var == "Data passed from index.ui.js gets value through GetData, can return JSON object directly \ n"        null, 2); // Formatting function Close_me () {    //  closes itself, passing the data back to the next layer of page    app.closepage ("I am from open_close_page/ Index.ui the data passed over when it is closed ");

A detailed example of the entire document is referenced here

Data sharing and data transfer

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.