The program applets the App constructor ()

onLaunch / onShow / onHide three callback is a function of the life cycle of instances App

"Applet" refers to a product level program, the "program" refers to a program example code level in order to avoid misunderstanding, hereinafter the App instead of "program" concept code level.

(1) Program the App constructor ()

Host environment provides App () constructor is used to register a program App, need to pay attention is the App () constructor must be written in the root directory of the project's app.js, App instance is a singleton object that can be used in other JS script getApp (provided by the host environment) to acquire program instance.

Examples of code gets App

// other.js
var appInstance = getApp()

App () is called by the code as shown in Listing, App Object constructor accepts a parameter 3-4, the parameters described in Table 3-1, wherein onLaunch / onShow / onHide three App callback function is an example of the life cycle, we will start later; onError we do not expand this chapter, we will discuss in detail in Chapter 8; other parameters of our App also be expanded on later.


Code App constructor

App({
  onLaunch: function(options) {},
  onShow: function(options) {},
  onHide: function() {},
  onError: function(msg) {},
  globalData: 'I am global data'
})

App constructor argument

Parameters property Types of description
onLaunch Function When the applet initialization is complete, it will trigger onLaunch (Global triggers only once)
onShow Function When the applet is started, or from the background into the foreground display will trigger onShow
onHide Function When the applet from the foreground into the background, it will trigger onHide
onError Function When a small script error occurs or API call fails, it will trigger an error message and bring onError
Other fields   Arbitrarily You can add any function or data to the Object parameter, the App can access this instance call back

 

Life Cycle (2) program and open the scene

When initial entry applet WeChat client initiates a good host environment, while get downloaded from a network or from a local cache code package applet, it is injected into the host environment, after the initialization is completed, the micro-channel client will give App examples of distributed onLaunch event, onLaunch App constructor method defined parameters will be called.
After entering the applet, the user can click to close the upper right corner, or press the Home key on mobile devices leave the applet, then applet has not been directly destroyed, we have a condition called " applets into the background state ", App onHide constructor method defined parameters will be called.
When micro letter back again, or to open a small program again, micro-channel client will "background" applet wake up, we have a condition called "applets into the foreground state", onShow App constructor method defined parameters will It is called.
We can see, App lifecycle is a micro-channel client receives the user's operation of the active trigger. In order to avoid confusion on the program, we should not take the initiative to call the lifecycle methods App instances from other code.
Open the micro-channel client applet There are many ways: from group chat sessions in the open, from small list of programs open, open sweep the two-dimensional code through micro letter, open the current applet from other applets, for different ways open, small programs sometimes require different service processing to do so will open the micro-channel client mode and bring onLaunch Options onShow call parameters, sample code, and detailed parameters such as inventory codes shown in table 3-2 and 3-5. You need to pay attention to the host environment applet in an iterative update process will increase the number of open scene, so to get the latest scene check out the official documentation Value Description: https://mp.weixin.qq.com/debug/wxadoc/dev/framework /app-service/app.html .

Listing 3-5 onLaunch and onShow parameters

App({
  onLaunch: function(options) { console.log(options) },
  onShow: function(options) { console.log(options) }
})

onLaunch, onShow parameters

Field Types of description
path String Open Page path applet
query Object Open the page parameter query applet
scene Number Open the applet scene value, please refer to the detailed scene value applet official documents
shareTicket String shareTicket, see the applet official documents
referrerInfo Object When the scene is opened by the applet, or from another public number or App, return this field
referrerInfo.appId String Sources applet or the public or the App number of appId, see instructions below
referrerInfo.extraData Object Sources applet pass over the data, scene = support during 1037 or 1038

The following scenario supports returning referrerInfo.appId

Scene value Scenes Meaning appId information
1020 No public profile Page list of programs related to the small number of public sources of return appId
1035 No public custom menu Returns the number of public sources appId
1036 App to share information card Returns the source application appId
1037 Small applet program opens Sources applet return appId
1038 Returned from another small program Sources applet return appId
1043 No public message template Returns the number of public sources appId

(3) global data applet

Speaking before the applet JS script is run JsCore threads, each page of each program has a small thread WebView to render, so applets when switching pages, JS script to run a small program logic layer is still in the same context JsCore thread.
In the above said example is a single App embodiment, different pages may thus directly share data through the App instance attributes. App may pass additional configuration parameters in order to achieve a global attributes of a global shared data.
Code applets global shared data

// app.js 
the App ({ 
  GlobalData: ' the I AM Global Data '  // global shared data 
})
 // other page script other.js 
var appinstance = getApp () 
the console.log (appInstance.globalData) // Output: I It is global data

At the same time, we pay special attention to that all the pages of the script logic are run on the same thread JsCore page using setTimeout or setInterval timer, then when jumping to other pages, these timers have not been cleared, it needs to be developed themselves to clean up when leaving the page.

 

 

(4) operating mechanism applet

    The applet starts there will be two cases , one is the "cold start" , one is "hot start" . If a user has opened the applet, the applet then open again within a certain time, this time without restarting, simply switching state applet background to the foreground, this process is the hot start; refers to a cold start user for the first time on or after case the applet is actively destroy micro-channel open again, this time to start a small program needs to be reloaded.

    Update mechanism: The applet cold start if found to have the new version will be asynchronous download the new version of the code package , while the local packet starts with the client , that is, the next cold start will use the new version of the applet to wait on .

  • Applet without rebooting concept
  • When the applet into the background , the client will remain operational status for some time , after more than a certain time ( currently 5 minutes ) will be actively destroy micro letter
  • Top applet will not be actively destroy micro letter
  • When received alarm system memory will be a small program to destroy

 

 

 

 

 

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/11121989.html