AS3 / Flex的4:最实用的方法查找嵌套子 [英] AS3/Flex 4: Most Practical Way To Find Nested Children

查看:109
本文介绍了AS3 / Flex的4:最实用的方法查找嵌套子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在跳头朝下一定的Flex / AIR的东西。我有一个pretty的坚实背景AS3,但由于Flex的内在层次的复杂性(相对于常规Flash),我遇到了一个问题。

I'm sort of jumping in headfirst to some Flex/AIR stuff. I have a pretty solid background with AS3, but given the inherent hierarchal complexity of Flex (compared to regular Flash), I'm running into an issue.

让我们假设你有一个应用程序,其中pretty的每样东西是事件驱动的(通用)。在事件目标,或事件目标本身的酒店附近访问的元素,实在是微不足道。我试图寻找,然而,最实用的(阅读:最好的,最有效的)的方式找到的孩子,从当前情况下相去甚远

Let's assume that you have an app where pretty much everything is event driven (common). Accessing elements in the near vicinity of the event target, or the event target itself, is trivial. I'm trying to find, however, the most practical (read: best, most efficient) way to find children that are far removed from the current context.

我知道有类似的功能 getChildAt() getChildByName(),但假设父上下文;如果该元素(Flex的),你要找的是几名家长了,在一个兄弟,然后几个孩子了吗?我们采取像jQuery理所当然的事情,这样做很容易,但显然我们不具备这种奢侈的AS3。

I know there are functions like getChildAt() and getChildByName(), but that assumes a parent context; what if the element (Flex) you're looking for is several parents up, in a sibling, and then several children down? We take for granted things like jQuery that do this easily, but obviously we don't have that luxury in AS3.

是下列任何有效吗?有没有更好的办法?

Are any of the following valid? Is there a better way?

  1. 遍历父母和父母的父母,直到你找到一个停止点,找到兄弟姐妹,以及遍历的孩子和他们的孩子,直到你找到你的目标;

  1. Iterate through parents and parents' parents until you find a stop point, find the sibling, and iterate through children and their children until you find your target;

请关键对象在全局对象存储(原文如此),并引用它们在必要时(yech)

Keep key objects in a global object store (sic) and reference them as necessary (yech)

使用特定的点表示法来达到目标​​,包括元素(如皮肤及其容器 - yech再次)

Use specific dot notation to reach the target, including elements (like skins and their containers - yech again)

任何想法将AP preciated。

Any thoughts would be appreciated.

编辑:

要澄清,让我们来一个空的Flex 4 AIR应用程序。我们有的WindowedApplication 为根,很明显,让我们添加两个 SkinnableContainer 小儿标识 navContainer mainContainer上,分别。两者都有自定义皮肤。在 mainContainer上,我们有另一个 SkinnableContainer 与垂直布局和ID 日程地址搜索Maincontent ,并作为其子之一,它拥有一个对象(任何会做 - 火花使用BorderContainer ,也许)与ID animatedBox 为例。在 navContainer ,我们有一个火花按钮,其中有开往为MouseEvent的听众。点击。在这一功能,我们将要访问 animatedBox nativeWindow.mainContainer.mainContent.animatedBox )和动画它改变,也就是说,它的宽度。

To clarify, let's take an empty Flex 4 AIR app. We have WindowedApplication as the root, obviously, and let's add two SkinnableContainer children with IDs navContainer and mainContainer, respectively. Both have custom skins. Within mainContainer, we have another SkinnableContainer with a vertical layout and ID mainContent, and as one of its children, it has an object (any will do - a spark BorderContainer, maybe) with the ID animatedBox, for example. Within the navContainer, we have a spark Button, which has a listener bound for MouseEvent.CLICK. Within that function, we are going to want to access animatedBox (nativeWindow.mainContainer.mainContent.animatedBox) and animate it to change, say, it's width.

我们的目标是进入那个遥远的的DisplayObject (animatedBox )的方式,是因为不显眼,高效为可能的话,同时仍符合Flex的,我显然还没有具备的标准。 :)

The goal is to access that distant DisplayObject (animatedBox) in a way that is as unobtrusive and efficient as possible, while still conforming to Flex standards that I clearly have yet to possess. :)

推荐答案

在我的实现是容易做到的(但是它在纯AS3):

in my implementation it is easy to do (however it's in pure AS3):

在显示对象,处理点击:

in display object which handles the click:

private function onClick(e:MouseEvent):void{
    Radio.broadcast(new CustomEvent(id, ..params));
}

在animatedBox:

in animatedBox:

Radio.addListener(id, new Reciever(uid, animate));

private function animate(e:CustomEvent) {
   //needed code and access of CustomEvent props you pass
}

UPD:

package lazylib.broadcast 
{
    /**
     * ...
     * @author www0z0k
     */
    public class Reciever 
    {
        private var id: String;
        private var toRun: Function;
        /*@param nm - unique listener id - required
         *@param fn - event handler function - required*/
        public function Reciever(nm:String, fn:Function) 
        {
            id = nm;
            toRun = fn;         
        }

        public function onEvent(e:* = null):String {
            if (e == null) { return id; }
            toRun(e);
            return id;
        }

        public function get ID():String { return id; }

    }

}

package lazylib.broadcast
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    /**
     * ...
     * @author www0z0k
     */
    public final class Radio extends EventDispatcher
    {
        private static var listeners: Object = new Object();
        private static var archive: Array = new Array();
        private static var forSlowpokes: Object = new Object();

        public static function get ForSlowpokes():Object { return forSlowpokes; }

        public static function addListener(type: String , listener: Reciever):Boolean {
            listeners['anchor'] = null;
            if (!listeners[type]) { 
                var o: Object = new Object();
                listeners[type] = o;
            }
            if (!listeners[type][listener.ID]) {
                listeners[type][listener.ID] = listener; 
                return true;
            }else {
                return false;
            }
        }

        public static function broadcast(evt: * , singleUse:Boolean = false):void {
            var type:String = (evt as Event).type;          
            if (listeners[type]) {
                var returned: Array = new Array();
                for (var i: String in listeners[type]) {
                    if(listeners[type][i]){
                        var fnRetVal: String = listeners[type][i].onEvent(evt);
                        returned.push(fnRetVal);
                    }else{
                        //trace("no listener for id = " + i + ' , type = ' + type);
                    }
                }

            }else {
                //trace("nobody's interested in : \"" + type + "\"");
            }
            if (singleUse) {
                forSlowpokes[type] = 'you missed it realtime';
                delete listeners[type];
            }
        }

        public static function clearDeadFuncs(namez:Object):void {
            for (var a:String in namez) {
                if (a != 'anchor') {
                    killListener(a, namez[a]);
                }
            }
        }

        public static function killListener(type: String , id: String):Boolean {
            if (!listeners[type]) { 
                //trace("there are no listeners for event : " + "\"" + type + "\"");
                return false;
            }else {
                if (!listeners[type][id]) {
                    //trace("there is no \"" + id + "\" listener for event : " + "\"" + type + "\"");
                    return false;
                }else {
                    listeners[type][id] = null;
                    //trace("removed listener \"" + id + "\" for event : " + "\"" + type + "\"");
                    var evt2kill: Number = 0;
                    for (var str: String in listeners[type]) {
                        if (listeners[type][str]) {
                            evt2kill++;
                        }
                    }
                    if (evt2kill == 0) {
                        delete listeners[type];
                        //trace("no more listeners for event : " + "\"" + type + "\"");
                        return true;
                    }
                    return true;
                }
            }
        }
    }
}

交付的;)

这篇关于AS3 / Flex的4:最实用的方法查找嵌套子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆