开发者

What can I do to cause the binding to update on use of TextInput's insertText()?

I have a simple TextInput derived control, that insert's some text on Ctrl+Shft+B:

package controls
{
    import flash.events.KeyboardEven开发者_运维百科t;
    import flash.ui.Keyboard;
    import spark.components.TextInput;

    public class MyTextInput extends TextInput
    {
        private const BAM:String = "BAM!";

        public function MyTextInput()
        {
            super();
            this.addEventListener(KeyboardEvent.KEY_DOWN, interceptKey);
        }

        protected function interceptKey(event:KeyboardEvent):void
        {
            if((event.keyCode == Keyboard.B) && event.ctrlKey && event.shiftKey)
            {
                // Insert some text on Ctrl+Shft+B
                event.preventDefault();
                this.insertText(BAM);
            }
        }
    }
}

And I have a simple Flex app that uses the control:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
    xmlns:controls="controls.*"
    width="230" height="120"
    >
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[

            [Bindable]
            private var text:String;
        ]]>
    </fx:Script>
    <controls:MyTextInput left="10" right="10" top="10" text="@{text}"/>
    <s:Label left="10" right="10" bottom="10" text="{text}"/>
</s:WindowedApplication>

The label field updates for single keystrokes but on invoking Ctrl+Shft+B, the expected text appears in the TextInput derived control, but not in the Label.


Be sure to dispatch the 'changed' event:

    protected function interceptKey(event:KeyboardEvent):void
    {
        if((event.keyCode == Keyboard.B) && event.ctrlKey && event.shiftKey)
        {
            // Insert some text on Ctrl+Shft+B
            event.preventDefault();
            this.insertText(BAM);
            dispatchEvent(new Event("textChanged"));
        }
    }

I assume the implementation for the insertText method does not use the set text method, which never fires the event, therefore never triggering binding.


Don't use insertText. I believe that's just for the visual appearance but never actually modifies the 'text' property. If anything replace insertText(BAM); with text += BAM;.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜