简体   繁体   English

Flex 4皮肤和装订

[英]Flex 4 skins & binding

I love skins in Flex 4 - they offer a really nice separation between logic and styling. 我喜欢Flex 4中的皮肤 - 它们在逻辑和造型之间提供了非常好的分离。

I was wondering how people use binding in skins. 我想知道人们如何在皮肤中使用绑定。 The easiest way is to just bind in the skin: 最简单的方法就是绑定皮肤:

label="{ hostComponent.model.buttonLabel }"

you can also use 2 way bindings: 你也可以使用双向绑定:

selectedItem="@{ hostComponent.model.selectedItem }"

but you could argue that this is logic so it should really be handled in the component. 但你可以说这是逻辑,所以它应该在组件中真正处理。 You don't really want to re-create all the bindings each time you create a new skin. 每次创建新外观时,您都不想重新创建所有绑定。 That seems fragile and prone to creating bugs. 这似乎很脆弱,容易产生错误。

So what are the alternatives? 那有什么选择呢? Creating a bucket load of event listeners I suppose. 我想,创建一堆事件监听器。 The adobe observe and observeValues tags could be useful as well I suppose. 我认为adobe observe和observeValues标签也很有用。

How do others handle this? 别人怎么处理这个?

I think it's a good and interesting question. 我认为这是一个很好而有趣的问题。 There are a couple of alternatives. 有几种选择。 I suppose you could inject the model into the skin and bind directly, but that seems wrong because I think the skin should be intimately connected with the component - if the component changes its reference to the model, it automatically changes in the skins. 我想你可以将模型注入皮肤并直接绑定,但这似乎是错误的,因为我认为皮肤应该与组件紧密相连 - 如果组件更改其对模型的引用,它会自动在皮肤中更改。 And anyway it only exaggerates the problem you mentioned. 无论如何,它只会夸大你提到的问题。

You can create ids in the skin and operate on them from the model. 您可以在皮肤中创建ID并从模型中对它们进行操作。 For example 例如

SKIN: label id="infoLabel"
COMPONENT: [SkinPart] var infoLabel:Label
creationComplete {
  BindingUtils.bindProperty(this, 'infoLabel', model, 'infoLabel');
}

This solves your problem. 这解决了您的问题。 No repetitive maintenance between skins of the same component. 在同一组件的皮肤之间不需要重复维护。 However using BindingUtils comes at a cost. 但是使用BindingUtils需要付出代价。 Firstly it's not as evident what is bound to what - using the {} way is just so much more obvious. 首先,它不是那么明显是什么 - 使用{}方式更加明显。 Secondly (I suppose this is actually just part of the first point) you might confuse less-able developers that have to read your code and figure out what's going on. 其次(我认为这实际上只是第一点的一部分)你可能会混淆不那么有能力的开发人员,他们必须阅读你的代码并弄清楚发生了什么。

EDIT: This assumes your component is in AS3. 编辑:这假设您的组件在AS3中。 If your component is in MXML, I guess you can just bind as you normally would in MXML. 如果你的组件是MXML,我想你可以像往常一样在MXML中绑定。

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

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