Skip to content

Instantly share code, notes, and snippets.

@htoooth
Last active August 19, 2022 06:40
Show Gist options
  • Select an option

  • Save htoooth/253a8face04fa7f70b25fbd5d3a258b5 to your computer and use it in GitHub Desktop.

Select an option

Save htoooth/253a8face04fa7f70b25fbd5d3a258b5 to your computer and use it in GitHub Desktop.
const globalData = getApp()._options.globalData;
const HPage = (lifeCycle) => {
const { onInit: _onInit, build: _build, onDestroy: _onDestroy } = lifeCycle
return Object.assign(lifeCycle, {
onInit(param) {
if (param) param = JSON.parse(param);
this.state = param || {};
this.globalState = globalData.localStorage || {};
_onInit &&
typeof _onInit == "function" &&
_onInit(this.state, this.globalState);
},
build() {
try {
_build &&
typeof _build == "function" &&
_build(this.state, this.globalState);
} catch (e) {
logger.error(e);
if (e.stack) e.stack.split("\n").forEach((l) => logger.error(`${l}`));
hmUI.createWidget(hmUI.widget.TEXT, {
x: 0,
y: 0,
w: DEVICE.WIDTH,
h: DEVICE.HEIGHT,
text_size: 10,
text: e + "\n" + (e.stack || ""),
});
}
},
onDestroy() {
globalData.localStorage = this.globalState;
_onDestroy &&
typeof _onDestroy == "function" &&
_onDestroy(this.state, this.globalState);
},
});
};
Page(
HPage({
// your code
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment