Store global variable attention points globally

Background: When making applets, there is a shop interface that calls on almost every page, so take it out and put it in App.js lower

But in the process, take notes to save a complete picture.
1. Because only in App.js Down, the show interface is adjusted.
On other subpages, global variables stored in the shop are used, so if this interface is not invoked successfully or asynchronously not synchronized quickly at this time, it will make the whole program..
2. Keep as few global variables as possible and keep them concise

Solve:

Make the global variable null initially, and then on the following pages, decide if it exists or not, and call it again if it doesn't existApp.jsShop interface under, otherwise it will not be called again ~.

//app.js
const sendAjax = require('./utils/sendAjax.js')
const config = require('./config.js')
const host = config.host
const companyKey = config.companyKey
const mapChange = require('./utils/mapChange.js')


App({
    onLaunch: function () {

        console.info('Applet started~')
        this.getCompanyInfo()
    },
    onShow: function (path) {
        console.log(path)
    },
    globalData: {
        companyData: null
    },

    /**
     * Get company information
     */
    getCompanyInfo (cb) {
        let that = this

        if (that.globalData.companyData) {
            cb && cb(that.globalData.companyData)
            return
        }

        let infoOpt = {
            url: '/samples/public/shop',
            data: {
                companyKey
            }
        }

        let infoCb = {}

        infoCb.success = data => {
            that.globalData.companyData = data.company
            cb && cb(data.company)
        }

        sendAjax(infoOpt, infoCb)
    },

    sendAjax,
    host,
    companyKey,
    mapChange
})

On the subpage, check to see if there is a company value, if not, call it againApp.jsInterfaces in

/**
     * Lifecycle Functions--Listen for Page Loading
     */
    onLoad: function (options) {

        App.getCompanyInfo(company => {
            this.setData({
                num,
                name: company.name,
                series: company.series
            })
        })


    },

Posted on Sat, 23 May 2020 13:40:16 -0400 by fire_cracker