简体   繁体   中英

when to use scripts in xml

being somewhat new to xml, I see examples where scripts are embedded within xml. Unless it's xhtml, I don't understand why you would mark a section as a script. For example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="150" styleName="userstory">

    <fx:Style source="styles/styles.css"/>

    <fx:Text x="5" y="5" width="275" height="135" text="{userStoryText}" fontFamily="notes" fontSize="24"/>

    <mx:Script>
        <![CDATA[
            public var userStoryText:String;

            private function handleDown(e:Event):void {
                this.startDrag();
            }

            private function handleUp(e:Event):void {
                this.stopDrag();
            }

            override protected function createChildren():void {
                super.createChildren();
                super.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
                super.addEventListener(MouseEvent.MOUSE_UP,handleUp);
            }
        ]]>
    </mx:Script>
</mx:Canvas>

http://forums.adobe.com/thread/644918

The above being purely xml, why would you use a script? Only certain languages make sense -- just javascript and adobe flex?

thanks,

Thufir

If i am not wrong, you are asking why mix code with XML?

I think you are bit confused, you can have your actionScript code in a seperate file and call them in your main MXML component file or use your actionscript code inside your MXML component itself.

You use two languages to write Flex applications: MXML and ActionScript. MXML is an XML markup language that you use to lay out user interface components. You also use MXML to declaratively define nonvisual aspects of an application, such as access to data sources on the server and data bindings between user interface components and data sources on the server.

http://livedocs.adobe.com/flex/3/html/help.html?content=mxml_2.html

Let me know if you have anymore queries, if your question is still un answered.

I'm not sure completely what you're asking.

Flex is a mix of ActionScript and MXML. MXML is an XML dialect, as you allude to a bit.

The Script tag is a way to include ActionScript code inside an MXML Component. There is a lot you can do in ActionScript that can't be done in MXML. From your example the script overrides a method and defines a public property on the method. Those two things can't be done in MXML.

The event handlers could, in theory, be implemented in-line in MXML; but it gets very cumbersome if your event handler has more than one line, or any conditional logic. I don't think it is possible to do comparisons with '>', '<', or '&' in MXML.

How else would you do it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM