简体   繁体   English

将类用作飞镖聚合物元素的属性

[英]Use a class as attribute for a dart polymer element

I have a polymer element, which needs to load some data from a web service. 我有一个Polymer元素,它需要从Web服务加载一些数据。

<gallery-pol loader=""></gallery-pol>

@CustomTag('gallery-pol')
class PolymerGallery extends PolymerElement {
  @published GalleryDataLoader loader;
  ...
}

I don't want to have implementation of the data loading in my polymer element as it shouldn't be fixed to a specific web service. 我不想在聚合物元素中实现数据加载,因为它不应该固定在特定的Web服务上。

What is the best way to pass an object which implements the actual data loading to my polymer element? 将实现实际数据加载的对象传递给我的聚合物元素的最佳方法是什么?

Should i just select my element in the main program and assign the loader the to element? 我应该只在主程序中选择我的元素并将加载程序分配给元素吗? Or is there some smother way to directly pass it as an attribute? 还是有一些更sm草的方法可以直接将其作为属性传递?

Thank you, lolsharp 谢谢你,lolsharp

I would consider modeling the loader as a custom element too. 我也考虑将加载程序建模为自定义元素。 From there you have a few options on how they communicate. 从那里,您可以选择几种方式进行交流。

One is to let the gallery find the loader as a child: 一种是让画廊将装载机作为孩子找到:

<z-gallery>
  <z-gallery-loader></z-gallery-loader>
</z-gallery>

Elements can also find each other via queries/ids or binding: 元素还可以通过查询/标识或绑定彼此找到:

<z-gallery-view>
  <z-gallery-loader id="loader"></z-gallery-loader>
  <z-gallery loader="{{ $['loader'] }}"></z-gallery>
</z-gallery-view>

$[] in the context of a <z-gallery-view> Polymer element finds content by id. <z-gallery-view> Polymer元素中的$[]通过id查找内容。 You could also bind to an id string and do the query yourself: 您还可以绑定到id字符串并自己执行查询:

<z-gallery-view>
  <z-gallery-loader id="loader"></z-gallery-loader>
  <z-gallery loader="loader"></z-gallery>
</z-gallery-view>

A different approach is to communicate via events: 另一种方法是通过事件进行通信:

<z-gallery-loader on-load-gallery="{{ loadGallery }}">
  <z-gallery></z-gallery>
</z-gallery-loader>

<z-gallery> can fire 'load-gallery' (or maybe some more user-action centric event) custom events to request data to be loaded, and since it's included as the target of the event, the loader can do it's work and push the data back to the gallery. <z-gallery>可以触发“ load-gallery”(或可能是其他一些以用户操作为中心的事件)自定义事件,以请求加载数据,并且由于将其包含为事件的目标,因此加载程序可以完成工作并将数据推回图库。

If you don't want the containment relationship there, you can redirect events by having a different parent element that triggers events on the loader: 如果您不希望包含关系,则可以通过具有另一个在加载程序上触发事件的父元素来重定向事件:

<z-gallery-view on-load-gallery="{{ loadGallery }}">
  <z-gallery-loader></z-gallery-loader>
  <z-gallery></z-gallery>
</z-gallery-view>

Where in this case loadGallery delegates to the loader child. 在这种情况下,loadGallery委托给loader子代。

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

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