聚合物全局变量 [英] Polymer global variables

查看:151
本文介绍了聚合物全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Polymer应用程序,它从RESTful API中提取数据并使用它来构建接口。我在概念上坚持的一个特定领域是 http://中描述的Monostate模式的实现。 www.polymer-project.org/docs/polymer/polymer.html#global 。实际上,我可以将声明性属性添加到新组件app-globals中,然后合理地直接访问它。

I'm working on a Polymer app, which is pulling data from a RESTful API and using it to construct the interface. A specific area I'm stuck on conceptually is the implementation of the Monostate Pattern described at http://www.polymer-project.org/docs/polymer/polymer.html#global. Effectively, I can add declarative attributes into a new component, app-globals, and then access this reasonably straightforwardly.

以下是关键问题:如果我通过core-ajax将数据来回(并且可能,重新提交)到app-globals组件中的API,如何确保app-globals组件的所有使用者拥有相同的数据?如果我使用建议的模式,似乎我已经失去了我的单调性:

Here's the key question: if I'm pulling (and, potentially, resubmitting) data back and forth via core-ajax to the API within the app-globals component, how do I ensure that all consumers of the app-globals component have the same data? Seems like I've lost my monostatism if I use the suggested pattern:

<polymer-element name="my-component">
  <template>
    <app-globals id="globals"></app-globals>
    <div id="firstname"></div>
    <div id="lastname"></div>
  </template>
  <script>
    Polymer('my-component', {
      ready: function() { this.globals = this.$.globals; }
     });
  </script>
</polymer-element>    

因为使用app-globals的每个组件都会提取自己版本的API数据。我错过了什么吗?有没有其他方法可以确保应用程序始终只有一个版本的事实?

because each of the components that consume app-globals will be pulling their own version of the API data. Am I missing something? Is there another way to ensure that the app constantly has only one version of the truth?

推荐答案

每次引用app-globals在自定义组件中,创建了一个新的app-globals实例。但是这些实例中的每一个都可以共享许多属性(想想Java中的静态变量或ObjC / Swift中的类变量)。

Each time you reference app-globals in a custom component, a new instance of app-globals is created. But each of these instances can share a number of properties (think "static" vars in Java or "class" vars in ObjC / Swift).

应用程序中的脚本globals元素(或实际上任何Polymer元素)只运行一次 - 在加载组件定义时将其视为运行。但该脚本中的Polymer函数声明了一个配置对象,其中包含属性和生命周期事件处理程序,这些对象将为每个实例单独创建。您引用的文档中的app-globals脚本(复制到UPDATE下面:此版本将在后面描述中修改)使用匿名闭包(一个立即运行的函数),包含共享变量和声明配置对象的Polymer函数反过来引用共享变量。因此,该配置对象的每个实例 - 以及每个app-globals实例 - 将引用同一组共享变量。

The Script within app-globals element (or indeed any Polymer element) runs only once - think of it as running when the component definition is loaded. But the Polymer function within that script declares a configuration object, with properties and lifecycle event-handlers, that will be created separately for each instance. The app-globals script in the document you reference (copied below UPDATE: this version is modified as described later) uses an anonymous closure (a function that is run immediately), containing both the shared variables and the Polymer function declaring the config object which in turn references the shared variables. So each instance of that config object - and in turn each instance of app-globals - will reference the same set of shared variables.

 <polymer-element name="app-globals">
  <script>
  (function() {
    var data = {
      firstName: 'John',
      lastName: 'Smith'
    }

    Polymer('app-globals', {
       ready: function() {
         this.data = data;
       }
    });
  })();
  </script>
</polymer-element>

如果一个自定义组件实例更改了其app-globals实例的值(或者它们在内部更改,作为你的案例中的AJAX调用的结果)所有其他组件实例与app-globals的引用将看到更改的值。

If one custom component instance changes a value on its app-globals instance (or they are changed internally, as the results of an AJAX call in your case) all other component instances with a reference to app-globals will see the changed value.

更新:原文,复制自:

http://www.polymer-project.org/docs/polymer/polymer.html#global

有缺陷,如@所述zreptil,如果更改数据值,则新值不可用于所有其他实例 - 因为实例变量只是引用字符串的副本。通过使用具有数据属性的对象(如上面编辑的版本),并且只读取和分配该对象的数据属性而不是覆盖对象本身,更改的值可在实例之间共享。

had a deficiency, as described by @zreptil, if you change the data values, the new values are NOT available to all other instances - because the instance variables are just copies of the referenced strings. By using an object with data properties, as in the edited version above, and only ever reading from and assigning to the data properties of that object rather than overwriting the object itself, changed values are shareable between instances.

这篇关于聚合物全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆