获取任何元素的变化

发布于 2024-11-08 01:00:26 字数 143 浏览 2 评论 0原文

我有一个带有一些组件(textInput、textArea、checkBox)的屏幕。我想检查一下组件是否有任何变化。例如,如果已输入文本或已单击复选框。

有没有直接的方法来做到这一点,而不是检查每个组件的值,然后为此设置一个布尔值?

谢谢

I have a screen with a few components (textInput, textArea, checkBox). I want to check if there has been any changes to the components. Foe example if text has been entered or checkbox has been clicked.

Is there a direct way to do this instead of checking value of each component and then setting a Boolean for this?

Thanks

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

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

发布评论

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

评论(3

温暖的光 2024-11-15 01:00:26

您列出的三个 Spark 组件(TextInput、TextArea、CheckBox)都侦听更改事件。只需调用一个函数即可在每个组件上发生更改事件时收到通知,如下所示:

<s/TextInput change="myTextInputChangeHandler(event)"/>
<s/TextArea change="myTextAreaChangeHandler(event)"/>
<s/CheckBox change="myCheckBoxChangeHandler(event)"/>

The three Spark components you listed (TextInput, TextArea, CheckBox) all listen for a change event. Simply call a function to be notified when change events occur on each component, like so:

<s/TextInput change="myTextInputChangeHandler(event)"/>
<s/TextArea change="myTextAreaChangeHandler(event)"/>
<s/CheckBox change="myCheckBoxChangeHandler(event)"/>
森末i 2024-11-15 01:00:26

您可以创建一些包含数据并跟踪更改的类。并使用两种方式绑定将控件与该数据对象字段绑定。

数据对象的基类如下所示:

public class DataObjectsBase extends EventDispatcher
{
    protected function setNewSetterValue(propertyName:String, value:Object):void
    {
        var internalPropertyName:String = "_" + propertyName;
        var oldValue:Object = this[internalPropertyName];
        if (oldValue == value)
            return;
        this[internalPropertyName] = value;
        dispatchUpdateEvent(propertyName, oldValue, value);
    }

    protected function dispatchUpdateEvent(propertyName:String, oldValue:Object, value:Object):void
    {
        makeChanges();
        dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, propertyName, oldValue, value));
    }

    protected function makeChanges():void
    {
        if (!_changed)
        {
            _changed = true;
            dispatchEvent(new Event("changedChanged"));
        }
    }

    public function resetChanges():void
    {
        _changed = false;
        dispatchEvent(new Event("changedChanged"));
    }

    private var _changed:Boolean = false;

    [Bindable("changedChanged")]
    public function get changed():Boolean
    {
        return _changed;
    }
}

此实现依赖于以下类型的命名约定:

public class MyDataImplementation extends DataObjectsBase
{
    private var _comment:String = "";

    [Bindable(event = "propertyChange")]
    public function get comment():String
    {
        return _comment;
    }

    public function set comment(value:String):void
    {
        setNewSetterValue("comment", value);
    }
}

因此私有字段的命名应与访问器相同,但以下划线开头。

如果您用多个字段(字符串、布尔值等)填充数据类,您可以按以下方式使用它:

<fx:Script>
<![CDATA[
    [Bindable]
    private var personalInfo:PersonalInfo /* extends our DataObjectsBase */;
]]>
</fx:Script>
<s:TextInput text="@{personalInfo.firstName}" />
<s:CheckBox selected="@{personalInfo.receivePromotions}" />
<s:Button label="Apply" enabled="{personalInfo.changed}" />

此代码只是一个草稿,但我希望您明白:)

You can create some class which contains data and tracks changes. And bind controls with this data object fields using two way binding.

The base class for you data object can look like the following:

public class DataObjectsBase extends EventDispatcher
{
    protected function setNewSetterValue(propertyName:String, value:Object):void
    {
        var internalPropertyName:String = "_" + propertyName;
        var oldValue:Object = this[internalPropertyName];
        if (oldValue == value)
            return;
        this[internalPropertyName] = value;
        dispatchUpdateEvent(propertyName, oldValue, value);
    }

    protected function dispatchUpdateEvent(propertyName:String, oldValue:Object, value:Object):void
    {
        makeChanges();
        dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, propertyName, oldValue, value));
    }

    protected function makeChanges():void
    {
        if (!_changed)
        {
            _changed = true;
            dispatchEvent(new Event("changedChanged"));
        }
    }

    public function resetChanges():void
    {
        _changed = false;
        dispatchEvent(new Event("changedChanged"));
    }

    private var _changed:Boolean = false;

    [Bindable("changedChanged")]
    public function get changed():Boolean
    {
        return _changed;
    }
}

This implementation relies on naming convention of the following kind:

public class MyDataImplementation extends DataObjectsBase
{
    private var _comment:String = "";

    [Bindable(event = "propertyChange")]
    public function get comment():String
    {
        return _comment;
    }

    public function set comment(value:String):void
    {
        setNewSetterValue("comment", value);
    }
}

So private field should be named the same as accessor but starting with underscore.

If you fill your data class with several fields (strings, booleans etc) you can use it the following way:

<fx:Script>
<![CDATA[
    [Bindable]
    private var personalInfo:PersonalInfo /* extends our DataObjectsBase */;
]]>
</fx:Script>
<s:TextInput text="@{personalInfo.firstName}" />
<s:CheckBox selected="@{personalInfo.receivePromotions}" />
<s:Button label="Apply" enabled="{personalInfo.changed}" />

This code is just a draft but I hope you get the idea :)

月光色 2024-11-15 01:00:26

您可以在每个组件上注册一个事件侦听器以自动侦听更改。

如果您试图弄清楚从特定时间(或最后检查的实例)是否发生了更改,唯一的方法(我知道)是存储先前的输入(输入的哈希值)并将其与当前输入(输入的哈希值)进行比较当前输入)。

You can register an event listener on each of the components to automatically listen for changes.

If you are trying to figure out if a change has occurred from a specific time (or last checked instance) the only way (I know) would be storing the previous input (hash of input) and comparing it to the current input (hash of the current input) .

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