preface

In the last summary, we know the micro channel small program framework, this summary, we come to know what micro channel small program logical layer.

Wechat applet logic layer

The logic layer of the development framework of wechat applets is written in Javascript. The logical layer processes data and sends it back to the view layer and receives event feedback from the view layer. In the process of writing, the Javascript based on the expansion. App() is used to register a small program, getApp() is used to get an instance of App(), getCurrentPages is used to get the current Page stack, and Page() is used to register a Page.

App() registers a small program

In app.js, use app () to register a small program, used to receive the parameters of an object, its function is to formulate the small program declaration cycle function, etc. Example code is as follows:

//app.js App({ onLaunch: The function () {/ / var logs to show local storage ability = wx getStorageSync (' logs') | | [] logs. The unshift (Date. Now ()) wx. SetStorageSync (' logs', // wx.login({success: Wx.getsetting ({success:) {// send res.code to backgroud for openId, sessionKey, unionId}}) If (res.authSetting[' scope.userinfo ']) {// If (res.authSetting['scope.userInfo']) { UserInfo = res.userInfo // Since getUserInfo is a network request, May be on the Page. The onLoad before return / / so I join the callback to prevent this kind of situation the if (this. UserInfoReadyCallback) {enclosing userInfoReadyCallback (res)}} }) } } }) }, globalData: { userInfo: null } })Copy the code

Page() Registers a Page

The Page() function is mainly used to register a Page. Receives an Object argument.

These are mainly functions that specify the initialization data, lifecycle functions, and event handling of the page. Example code is as follows:

Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: Wx.caniuse ('button.open-type.getUserInfo')}, // bindViewTap: function() {wx.navigateto ({url: '.. /logs/logs' }) }, onLoad: function () { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: } else if (this.data.caniuse){// Since getUserInfo is a network request, May be on the Page. The onLoad before return / / so I join the callback to prevent this app. UserInfoReadyCallback = res = > {enclosing setData ({the userInfo: Res.userinfo, hasUserInfo: true})}} else {// Handle wx.getUserInfo({success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } }, getUserInfo: function(e) { console.log(e) app.globalData.userInfo = e.detail.userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) } });Copy the code

Today is the Lantern Festival, I wish everyone a happy Lantern Festival ~ unfinished, to be continued…