简体   繁体   English

由客户端调用服务器功能设置的服务器端变量是否持久? (没有!)

[英]Server-side variables set by client call to server function persist? (No!)

I tried to use the Revealing module pattern in apps javascript. 我试图在应用javascript中使用显示模块模式 I like the idea, but what it seems to have most acutely revealed is my ignorance of 1) javascript 2) apps scripts and 3) how they interact. 我喜欢这个主意,但似乎最清楚地揭示了我对1)javascript 2)应用程序脚本以及3)它们如何交互的无知。

That pattern works fine when running a function in the editor (see test() below), but when run from a live webapp, it breaks down. 在编辑器中运行功能时,该模式可以正常工作(请参见下面的test()),但是从实时Web应用程序运行时,它会崩溃。 I have the impression that values in the server-side javascript cannot be set using closures. 我的印象是无法使用闭包设置服务器端javascript中的值。

Whenever I run getEmail or setEmail in the editor, things work fine. 每当我在编辑器中运行getEmail或setEmail时,一切正常。 If I call them from a webapp, the values set by setEmail do not remain set, so another call to the getEmail will retrieve the original values and not the values that were set by setEmail. 如果我从Web应用程序调用它们,则setEmail设置的值不会保持设置,因此对getEmail的另一次调用将检索原始值,而不是setEmail设置的值。

If the same javascript code were in the html page as a script, the settings would stick, I believe. 我相信,如果html页面中的脚本与JavaScript代码相同,则设置会保留下来。 (ok... will have to test that...). (好的...必须测试一下...)。

The working code: https://script.google.com/macros/s/AKfycbwrmi-b7J5Gqb_SDUUuiO-TTG31hQJVWtMLFvAPTPOb97qZaQw/exec 工作代码: https : //script.google.com/macros/s/AKfycbwrmi-b7J5Gqb_SDUUuiO-TTG31hQJVWtMLFvAPTPOb97qZaQw/exec

The code itself: https://script.google.com/d/1LNw54M-hMgafMfsnYBR0QzFlzXDhd9mFX4asghtqejJRC2Uh66zoFuAb/edit?usp=sharing 代码本身: https : //script.google.com/d/1LNw54M-hMgafMfsnYBR0QzFlzXDhd9mFX4asghtqejJRC2Uh66zoFuAb/edit?usp=sharing

Core parts: 核心部分:

function doGet() {
  settings.setEmail("start email");
  return HtmlService.createTemplateFromFile('dialog')
            .evaluate()
            .setSandboxMode(HtmlService.SandboxMode.NATIVE)
}

var settings = (function () {
        var email = "blank";
        var getEmail= function () { return email}
        var setEmail= function (m) { email=m}

        return {
          getEmail: getEmail,
          setEmail: setEmail
        }
} ());

function getVal() { return settings.getEmail() }
function setVal(x) { settings.setEmail(x); return "set '" + x + "'"}

function test() {
  Logger.log(setVal("my email"));
  Logger.log(getVal());
}

Html code: HTML代码:

    <div id="output" >  <?= settings.getEmail()?> </div>
    <br>
    <input  id="input" size="20" type="string" />
    <input type="button" value="Set Value"  onclick="google.script.run.withSuccessHandler(serverSaid).setVal(document.getElementById('input').value);" />
    <input type="button" value="Get Value"  onclick="google.script.run.withSuccessHandler(serverSaid).getVal();" />

    <script>
    function serverSaid(reply) {
          var div = document.getElementById('output');
          var messages= div.innerHTML;
          div.innerHTML = messages +" | " + reply;
    }
    </script>

So, I tried doing the same thing client-side. 因此,我尝试在客户端执行相同的操作。 It works as expected. 它按预期工作。 Here is the HTML code: 这是HTML代码:

<div id="output" >  </div>
<br>
<input  id="input" size="20" type="string" />
<input type="button" value="Set Value"  onclick="serverSaid(setVal(document.getElementById('input').value));" />
<input type="button" value="Get Value"  onclick="serverSaid(getVal());" />

<script>
function serverSaid(reply) {
      var div = document.getElementById('output');
      var messages= div.innerHTML;
      div.innerHTML = messages +" | " + reply;
}

/// everything below has been moved over from server-side 
var settings = (function () {
        var email = "blank";
        var getEmail= function () { return email}
        var setEmail= function (m) { email=m}

        return {
          getEmail: getEmail,
          setEmail: setEmail
        }
} ());

function getVal() { return settings.getEmail() }
function setVal(x) { settings.setEmail(x); return "set '" + x + "'"}

</script>

I think you misunderstood some things. 我想你误会了一些事情。 If I understand well your problem, you are trying to call some server side functions to set/get a value. 如果我很了解您的问题,则您正在尝试调用一些服务器端函数来设置/获取值。 But you actually don't store this value server side ! 但是实际上您不存储此值服务器端! Differences you get is because of different contexts. 您得到的差异是由于不同的上下文。

When you use the editor, the script is executed in this own context, meaning all instantiated objects/variables will stay as long as the script is executed (as long as you stay on the page). 使用编辑器时,脚本将在此上下文中执行,这意味着所有实例化的对象/变量都将在脚本执行后停留(只要您停留在页面上)。

But when you call these functions from client side, the server script will be executed once after a call and then its context will be destroyed as you "leave" (end of the execution of the function) the server. 但是,当您从客户端调用这些函数时,服务器脚本将在调用后执行一次,然后在您“离开”服务器(函数执行结束)时破坏其上下文。

So the value is actually changed, but the context where the value is changed is destroyed after the end of the execution. 因此,实际上更改了该值,但是在执行结束后破坏了更改该值的上下文。 Finally, when you call the get function, you recreate a brand new context where the value is instantiated with its default value. 最后,当调用get函数时,您将重新创建一个全新的上下文,在该上下文中将使用其默认值实例化该值。

To handle this, you have to store the variable somewhere, into a database, for example. 要处理此问题,您必须将变量存储在某个地方,例如到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM