ArrayCollection 包装器允许绑定机制查看更改
我有一个包含可绑定对象的 ArrayCollection。我想在多个地方访问它们,如下所示(为了清楚起见,只有一个标签)。我怎样才能做到这一点?我应该为 ArrayCollection 编写某种包装器来调度可由 Flex 事件机制捕获的特殊事件(哪个?)。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<mx:VBox>
<s:Label id="lb" text="{lab(col)}" />
<s:Button click="onC(event)" color="0x000000"/>
</mx:VBox>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.PropertyChangeEvent;
import spark.events.ListEvent;
[Bindable] private var col:ArrayCollection;
private function init():void {
col = new ArrayCollection();
col.addItem({ val: 1 });
col.addItem({ val: -1 });
}
private function onC(event:MouseEvent):void {
col.getItemAt(0).val++;
trace(col.getItemAt(0).val);
}
private function lab(_:ArrayCollection):String {
for(var i:int = 0; i < col.length; i++) {
if(col.getItemAt(i).val > 0)
return col.getItemAt(i).val.toString();
}
return "-666";
}
]]>
</fx:Script>
</s:Application>
我想创建这样的包装器:
public class BindableArrayCollection extends EventDispatcher {
[Bindable] private var _ac:ArrayCollection;
public function BindableArrayCollection() {
this.ac = new ArrayCollection();
}
[Bindable]
public function set ac(val:ArrayCollection):void {
if(_ac != null)
_ac.removeEventListener(CollectionEvent.COLLECTION_CHANGE, toggleChangeEvent);
_ac = val;
if(_ac != null)
_ac.addEventListener(CollectionEvent.COLLECTION_CHANGE, toggleChangeEvent);
toggleChangeEvent();
}
public function get ac():ArrayCollection {
return _ac;
}
public function toggleChangeEvent(_:Object = null):void {
dispatchEvent(....);
}
}
以便绑定机制能够工作。我应该用什么来代替“...”?
I have an ArrayCollection containing bindable objects. I would like to access them in multiple places like below (there's only one label for clarity). How can I achieve that? Should I write some kind of wrapper for ArrayCollection dispatching special event (which?) catchable by Flex event mechanism.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<mx:VBox>
<s:Label id="lb" text="{lab(col)}" />
<s:Button click="onC(event)" color="0x000000"/>
</mx:VBox>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.PropertyChangeEvent;
import spark.events.ListEvent;
[Bindable] private var col:ArrayCollection;
private function init():void {
col = new ArrayCollection();
col.addItem({ val: 1 });
col.addItem({ val: -1 });
}
private function onC(event:MouseEvent):void {
col.getItemAt(0).val++;
trace(col.getItemAt(0).val);
}
private function lab(_:ArrayCollection):String {
for(var i:int = 0; i < col.length; i++) {
if(col.getItemAt(i).val > 0)
return col.getItemAt(i).val.toString();
}
return "-666";
}
]]>
</fx:Script>
</s:Application>
I would like to create wrapper like this:
public class BindableArrayCollection extends EventDispatcher {
[Bindable] private var _ac:ArrayCollection;
public function BindableArrayCollection() {
this.ac = new ArrayCollection();
}
[Bindable]
public function set ac(val:ArrayCollection):void {
if(_ac != null)
_ac.removeEventListener(CollectionEvent.COLLECTION_CHANGE, toggleChangeEvent);
_ac = val;
if(_ac != null)
_ac.addEventListener(CollectionEvent.COLLECTION_CHANGE, toggleChangeEvent);
toggleChangeEvent();
}
public function get ac():ArrayCollection {
return _ac;
}
public function toggleChangeEvent(_:Object = null):void {
dispatchEvent(....);
}
}
so that the binding mechanism would work. What should I place instead of "...."?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是标签没有显示 ArrayCollection 中的值,对吗?之所以如此,是因为
Label
没有像DataGrid
或List
那样的 ArrayCollection 更新机制。您的 ArrayCollection 是可绑定的,但这仅意味着您的
lab(col)
函数仅在您更改col
变量值时才会执行。您设置了
col = new ArrayCollection();
,此时绑定被执行。只有在那之后,您才能为您的收藏添加价值。有两种方法可以解决此问题:lb.executeBindings()
来更新标签视图。CollectionEvent.COLLECTION_CHANGE
。希望这会对您有所帮助。
Your problem is that label is not showing value that is in your ArrayCollection, am I right? It's so, because
Label
does not have updating mechanism for ArrayCollection likeDataGrid
orList
.Your ArrayCollection is bindable, but this only means, that your
lab(col)
function will be executed only when you change yourcol
variable value.You set
col = new ArrayCollection();
and at this moment binding is executed. And only after that you add values to your collection. There are 2 ways of solving this problem:lb.executeBindings()
to update label view.CollectionEvent.COLLECTION_CHANGE
that ArrayCollection dispatch, when it's items are changed.Hope, this will help you.
看来你要开始了。我在这里看到的唯一可绑定的是 ArrayCollection,而不是其中的对象。如果您想让一个集合(或一般的任何数据)可以在应用程序中的任何位置访问,那么这些客户端代码片段需要知道如何访问它。一种可能的方法是实现单例设计模式 - http://www.squidoo .com/flash-tutorials_as3-singleton-design-pattern 这将是一个很好的起点,让您的数据变得更加“全球化”。
Looks like you are starting out. The only thing I see here bindable is the ArrayCollection, not the objects in it. If you want to make a collection (or any data in general) to be accessible anywhere in your application, those client pieces of code need to know how to get to it. One possible approach would be to implement the Singleton Design Pattern - http://www.squidoo.com/flash-tutorials_as3-singleton-design-pattern That would be a good starting point to get your data to be more "global" like.