简体   繁体   English

绑定到Polymer中的全局变量

[英]Binding to global variables in Polymer

I am fairly new to Polymer, and only just studied how data binding works. 我对Polymer相当陌生,只研究了数据绑定的工作原理。 I am porting an existing Dojo application, where there would be: 我正在移植现有的Dojo应用程序,该应用程序将在以下位置进行:

1) A single store (holding data) for each URL 1)每个URL都有一个商店(保存数据)

2) A message from the server when a store element was updated 2)更新商店元素时来自服务器的消息

As a result, a dynamically made select box which depended on data in the store would automagically have an extra item if anywhere in the application a user added an item to the store (which held the data) 结果,如果用户在应用程序中的任何位置向存储库中添加了项目(保存数据),则依赖于存储库中数据的动态创建的选择框将自动具有额外的项目。

I am trying to replicate something like this in Polymer. 我正在尝试在Polymer中复制类似的内容。

Use case: imagine an URL like this: /app/categories . 用例:想象这样的URL: /app/categories By querying it with an HTTP GET, you end up with a JSON of all the categories available. 通过使用HTTP GET查询它,您将得到所有可用类别的JSON。 While the app is running, the server might notify of a new element in /app/categories. 当应用程序运行时,服务器可能会通知/ app / categories中的新元素。 as a result, all of the selects in the application would then have the extra item automagically. 结果,应用程序中的所有选择将自动具有多余的项。

In Polymer, I learned how to bind a property in the current element to a property in a contained element . 在Polymer中,我学习了如何将当前元素中的属性绑定到包含元素中的属性。 I understand how this happens with the right events being fired etc. So, the idea would be to create a select where the items are generated by a dom-repeat , which would be bound to... a somehow global variable (?). 我了解在触发正确的事件等情况下这是如何发生的。因此,该想法将是创建一个选择,其中选择项由dom-repeat生成,而dom-repeat绑定到...某种程度上是全局变量(?)。

But... is it even possible to bind the property of a contained element to a "global" variable, rather than a property of the containing element? 但是...甚至可能将包含元素的属性绑定到“全局”变量,而不是包含元素的属性?

OR, more broadly, is there a pattern (or even an established pattern) to make sure that when a global variable is changed (thanks to a comet message, or whatever), a bunch of elements binding to it will be notified and therefore changed (in this case, the "select" using dom-repeat to show the items!) 或者,更广泛地说,是否存在一种模式(甚至是既定模式)来确保在更改全局变量(由于彗星消息或其他原因)时,将通知并绑定到其上的一堆元素(在这种情况下,使用dom-repeat进行“选择”以显示项目!)

Here is a JSBin that shows how to create a menu via iron-ajax At this point what's missing to the JSBin is the simulation of a server push about a data change, as well as a way to have all of the menus update at the same time. 这是一个JSBin,它显示了如何通过iron-ajax创建菜单。此时,JSBin缺少的是模拟有关数据更改的服务器推送,以及同时更新所有菜单的方式时间。

IMHO the most sane and maintainable pattern is a one way data flow approach (something along the lines of Flux or Redux (which is a bit simpler). 恕我直言,最明智和可维护的模式是一种单向数据流方法(类似于FluxRedux (有点简单)。

There is one global state object that exists as as a graph structure and the data flows down your component tree (starting with the root component). 有一个全局状态对象以图形结构的形式存在,并且数据沿组件树(从根组件开始)流下。
Each component has well defined input properties and receives data from the parent component (using data-binding) and passes on parts of the data to its children. 每个组件都具有定义明确的输入属性,并从父组件接收数据(使用数据绑定),并将部分数据传递给它的子组件。 When a component changes state, it fires an event which bubbles up the component tree up to your root component that can then update your global state object (which again flows down your component tree), communicate with the backend, etc. 当组件更改状态时,它会触发一个事件,该事件将组件树冒泡到您的根组件,然后可以更新您的全局状态对象(该对象又向下流到您的组件树),与后端进行通信等。

I can also recommend this video from the Polymer 2015 summit, which explains a similar approach (mediator pattern). 我也可以从Polymer 2015峰会上推荐此视频 ,该视频解释了类似的方法(调解人模式)。

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

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