flex ComboBox 的双向绑定?
Bidirectional binding for flex ComboBox?
我有一个集合,我想将其绑定为 ComboBox
:
的数据输入
private static var LOGOS:ArrayCollection = new ArrayCollection([
{index:0, label=logo1},
{index:1, label=logo2}
]);
<s:ComboBox selectedItem="@{model.labelIndex}" labelField="@label" dataProvider="{LOGOS}" />
Now, when selecting an item, the binding should send the associated index
属性 of the objext to the model and update labelIndex
.
当然它不像上面那样工作,因为 labelIndex
与 ArrayCollection
.
的数据类型不同
[Bindable]
private var model:MyModel;
[Bindable]
class MyModel {
public var:Number labelIndex;
}
问题:如何将数组元素映射到模型,反之亦然?
您正在寻找的内容需要一些脚本,绑定不够智能,无法自行解决这个问题。
您可以使用 BindingUtils
class 定义绑定,并使用 bindProperty
方法的 chain
参数修改查找值的方式。
对于combo.selectedItem
到model.labelIndex
的绑定,您可以将链指定为数组,其中元素定义查找值的路径:
BindingUtils.bindProperty(model, 'labelIndex', combo, ['selectedItem', 'index']);
这将绑定到 selectedItem
属性,并传递项目 index
属性.
的值
另一种方法有点棘手,需要使用 getter 函数根据 labelIndex
值从数据源中获取对象:
BindingUtils.bindProperty(combo, 'selectedItem', model, {
name: 'labelIndex',
getter: function(host:MyModel):Object
{
return LOGOS.source.filter(function(item:Object, index:int, array:Array):Boolean
{
return item.index === host.labelIndex;
})[0];
}
});
这将绑定到 labelIndex
属性,当 属性 更改时将调用 getter
函数。该函数将根据模型更改的 labelIndex
属性 值和 return 具有匹配 index
属性 值的源对象过滤数据源,最终将为组合框设置 selectedItem
属性.
您的组合框定义当然需要 id
才能通过脚本进行定位
<s:ComboBox id="combo" dataProvider="{LOGOS}" />
请注意,labelField
属性 中不需要 @
,这仅用于需要定位属性的 XML 数据源。但是,实际上您根本不需要指定它,因为 label
是 labelField
属性.
的默认值
我有一个集合,我想将其绑定为 ComboBox
:
private static var LOGOS:ArrayCollection = new ArrayCollection([
{index:0, label=logo1},
{index:1, label=logo2}
]);
<s:ComboBox selectedItem="@{model.labelIndex}" labelField="@label" dataProvider="{LOGOS}" />
Now, when selecting an item, the binding should send the associated index
属性 of the objext to the model and update labelIndex
.
当然它不像上面那样工作,因为 labelIndex
与 ArrayCollection
.
[Bindable]
private var model:MyModel;
[Bindable]
class MyModel {
public var:Number labelIndex;
}
问题:如何将数组元素映射到模型,反之亦然?
您正在寻找的内容需要一些脚本,绑定不够智能,无法自行解决这个问题。
您可以使用 BindingUtils
class 定义绑定,并使用 bindProperty
方法的 chain
参数修改查找值的方式。
对于combo.selectedItem
到model.labelIndex
的绑定,您可以将链指定为数组,其中元素定义查找值的路径:
BindingUtils.bindProperty(model, 'labelIndex', combo, ['selectedItem', 'index']);
这将绑定到 selectedItem
属性,并传递项目 index
属性.
另一种方法有点棘手,需要使用 getter 函数根据 labelIndex
值从数据源中获取对象:
BindingUtils.bindProperty(combo, 'selectedItem', model, {
name: 'labelIndex',
getter: function(host:MyModel):Object
{
return LOGOS.source.filter(function(item:Object, index:int, array:Array):Boolean
{
return item.index === host.labelIndex;
})[0];
}
});
这将绑定到 labelIndex
属性,当 属性 更改时将调用 getter
函数。该函数将根据模型更改的 labelIndex
属性 值和 return 具有匹配 index
属性 值的源对象过滤数据源,最终将为组合框设置 selectedItem
属性.
您的组合框定义当然需要 id
才能通过脚本进行定位
<s:ComboBox id="combo" dataProvider="{LOGOS}" />
请注意,labelField
属性 中不需要 @
,这仅用于需要定位属性的 XML 数据源。但是,实际上您根本不需要指定它,因为 label
是 labelField
属性.