[英]Google Apps Script Webapp async loading
I am writing a webapp using Google Apps Script.我正在使用 Google Apps Script 编写一个网络应用程序。
To reduce loading times I let doGet(e)
load a small file with some javascript to asynchronously load other JS and CSS.为了减少加载时间,我让
doGet(e)
加载一个带有一些 javascript 的小文件来异步加载其他 JS 和 CSS。
Loading external resources works fine of course:加载外部资源当然可以正常工作:
<head>
<!-- loading external resources works fine of course -->
<script src="https://cdnjs.cloudflare.c[...]/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.clou[...].1/jquery-ui.min.css">
</head>
But from what I know this cannot be done with code inside my apps script project because I cannot provide a direct link to the file.但据我所知,这不能用我的应用程序脚本项目中的代码来完成,因为我无法提供指向该文件的直接链接。 That's why I add a little
<script>
:这就是我添加一点
<script>
:
function loadScript(filePath) {
google.script.run.withSuccessHandler(function(returnedValueFromGAS) {
$('body').append(returnedValueFromGAS);
}).loadScript(filePath);
loadScript('someScriptFilepath');
loadScript('someStyleFilepath')
}
In this manner I add <script>
and <style>
tags to my HTML.通过这种方式,我将
<script>
和<style>
标签添加到我的 HTML 中。 And I would like to have not one callback for every loaded file, but one callback when all my (script-)files are loaded.而且我不希望每个加载的文件都有一个回调,而是在加载我的所有(脚本)文件时有一个回调。
This works fine so far with one major drawback: The Window-Load-Event is not of any use anymore.到目前为止,这工作正常,但有一个主要缺点: Window-Load-Event不再有任何用处。
How can I load JS and CSS files in the <head>
like I would do in other environments so that the load-event
still is of use for me?如何像在其他环境中那样在
<head>
加载 JS 和 CSS 文件,以便load-event
仍然对我有用?
And I would like to have not one callback for every loaded file, but one callback when all my (script-)files are loaded.
而且我不希望每个加载的文件都有一个回调,而是在加载我的所有(脚本)文件时有一个回调。
/**
* Create a promise around old callback api
* @param {String} func The server function to call
* @param {Object} args The arguments to the server function
* @return Promise
*/
const GSR = (func,...args) =>
new Promise((resolve,reject)=>
google.script.run
.withSuccessHandler(resolve)
.withFailureHandler(reject)[func](...args)
);
const bodyAppend = function(returnedValueFromGAS) {
$('body').append(returnedValueFromGAS);
}
//call GSR and append
const GSRa = (func, ...args) =>
GSR(func, ...args)
.then(bodyAppend)
.catch(e=>console.error(e.message));
function loadScriptClient() {//modified
//Can also use async/await here
Promise.all([GSRa('loadScript','someScriptFilepath'), GSRa('loadScript','someStyleFilepath')])
.then(/*All Files loaded and appended at this point; Provide a final callback function here*/)
.catch(e=>console.error(e.message));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.