将 Flex 组件绑定到类函数

发布于 2024-08-03 18:58:34 字数 1056 浏览 1 评论 0原文

我有几个组件,我想根据将用户名传递给函数来启用按钮。我想动态绑定按钮上的“enabled”属性,以便如果触发“somethingChanged”事件,按钮可能会启用或禁用。

但是,我不确定在哪里触发“somethingChanged”事件。我可能需要从应用程序中的多个位置触发“somethingChanged”事件。这可以通过绑定静态函数实现吗?

我是否以正确的方式处理这个问题,或者有更好的解决方案?

EventManager.as

public class EventManager():void
{
    [Bindable(event="somethingChanged")]
    public static function hasAccess(myVal:String):Boolean
    {
    }
}

testform1.mxml

<s:Button id="testButton1" enabled="{EventFunction.hasAccess("john")}" />
<s:Button id="testButton2" enabled="{EventFunction.hasAccess("mary")}" />
<s:Button id="testButton3" enabled="{EventFunction.hasAccess("beth")}" />

testform2.mxml

<s:Button id="testButton4" enabled="{EventFunction.hasAccess("tom")}" />
<s:Button id="testButton5" enabled="{EventFunction.hasAccess("bill")}" />
<s:Button id="testButton6" enabled="{EventFunction.hasAccess("jerry")}" />

I have several components where I want to enable buttons based on passing a username to a function. I want to dynamically bind the "enabled" property on a button so that if the "somethingChanged" event fires, a button may become enabled or disabled.

But, I'm not sure where to fire the "somethingChanged" event. It's possible that I may need to fire the "somethingChanged" event from several places in the application. Is this possible with a bound static function?

Am I going about this the right way or is there a better solution?

EventManager.as

public class EventManager():void
{
    [Bindable(event="somethingChanged")]
    public static function hasAccess(myVal:String):Boolean
    {
    }
}

testform1.mxml

<s:Button id="testButton1" enabled="{EventFunction.hasAccess("john")}" />
<s:Button id="testButton2" enabled="{EventFunction.hasAccess("mary")}" />
<s:Button id="testButton3" enabled="{EventFunction.hasAccess("beth")}" />

testform2.mxml

<s:Button id="testButton4" enabled="{EventFunction.hasAccess("tom")}" />
<s:Button id="testButton5" enabled="{EventFunction.hasAccess("bill")}" />
<s:Button id="testButton6" enabled="{EventFunction.hasAccess("jerry")}" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2024-08-10 18:58:34

像这样的东西会起作用。

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class EventManager extends EventDispatcher
    {
        [Bindable(event="somethingChanged")]
        public var hasAccess:Boolean = true;

        public static var instance:EventManager = new EventManager();

        public static function setHasAccess(hasAccess:Boolean):void
        {
            instance.hasAccess = hasAccess;
            instance.dispatchEvent(new Event("somethingChanged"));
        }
    }
}

可以这样使用:

<?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/halo">

    <fx:Script>
        <![CDATA[

            protected function button1_clickHandler(event:MouseEvent):void
            {
                EventManager.setHasAccess( false );
            }

        ]]>
    </fx:Script>

    <mx:Button click="button1_clickHandler(event)" enabled="{EventManager.instance.hasAccess}"/>
</s:Application>

我不知道这种方法是不是一个好的方法。这种类型的功能在适当的 MVC 结构中可能会得到更好的服务。这本质上是创建一个单例。要按照您所描述的方式实现它,我想您可能需要每个用户某种实例。不过,您将无法绑定到静态函数。

Something like this would work.

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class EventManager extends EventDispatcher
    {
        [Bindable(event="somethingChanged")]
        public var hasAccess:Boolean = true;

        public static var instance:EventManager = new EventManager();

        public static function setHasAccess(hasAccess:Boolean):void
        {
            instance.hasAccess = hasAccess;
            instance.dispatchEvent(new Event("somethingChanged"));
        }
    }
}

Which would be used like so:

<?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/halo">

    <fx:Script>
        <![CDATA[

            protected function button1_clickHandler(event:MouseEvent):void
            {
                EventManager.setHasAccess( false );
            }

        ]]>
    </fx:Script>

    <mx:Button click="button1_clickHandler(event)" enabled="{EventManager.instance.hasAccess}"/>
</s:Application>

I don't know that this approach is a good one. This type of functionality would probably be better served inside a proper MVC structure. This is essentially creating a singleton. To implement it as you describe, you'd probably want some sort of instance per user I suppose. You aren't going to be able to bind to a static function though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文