5 Replies Latest reply on Aug 25, 2009 8:17 AM by matt_chotin

    MXML, difference between "{object.method()}" and "object.method()"?

    viatropos Level 1

      How does that get figured out?  Like if I want a to assign a function variable a function value (i.e. var method:Function = myFunction), how/where does Flex figure that out, from "{object.method()}" and "object.method()"?

       

      If I create my own IMXMLObject, do I have to implement the string version of that call myself?

       

      Thanks,

      Lance

        • 1. Re: MXML, difference between "{object.method()}" and "object.method()"?
          matt_chotin Level 3

          If the compiler sees a property of type Function it expects that the string passed in as an attribute will be a function pointer.  Note that it should be object.method, not object.method().  If you do use {object.method()} then it is expected that method() returns a Function and the binding will execute once to get it.

           

          Matt

          1 person found this helpful
          • 2. Re: MXML, difference between "{object.method()}" and "object.method()"?
            viatropos Level 1

            Nice.

             

            So what if it's untyped (Object or *), and you want it to be either a Function or an Array?  The use-case here is I'm trying to set an "arguments" property on an object to an array, but it can either be an explicit Array or a Function, but I don't want to use curly brackets.  The Function would allow you to get arguments when the method object was invoked, while an Array would define them at initialization (you may want one or the other depending).  So something like this:

             

             

            <Script>

                <![CDATA[

            private function getArguments():Array {

            return [this, that];

            }

                ]]>

            </Script>

             

             

            <MethodObject id="wrapsAMethod" target="someOtherObjectId" method="someMethodOnTheTarget" arguments="getArguments"/>

             

            Will that work?  I would have usually done it like this:

             

            <MethodObject id="wrapsAMethod" target="{someOtherObjectId}" method="someMethodOnTheTarget" arguments="{getArguments}"/>

             

             

            But that creates a bunch of Binding objects in the background which I don't need.  The MethodObject would look like this:

             

            public class MethodObject

            {

            public var method:*;

            public var target:*;

            public var arguments:*;

             

            public function MethodObject() {

            super();

            }

             

            public function execute():void {

            var realArguments:Array = (this.arguments is Function) ? (this.arguments as Function)() : this.arguments as Array;

            var realMethod:Function;

            if (method is Function) {

            realMethod = (method as Function);

            } else {

            var realTarget:* = (target is Function) ? (target as Function)() : target;

            realMethod = realTarget[method];

            }

            realMethod.apply(null, realArguments);

            }

            }

             

            ... Basically everything is untyped, so you don't have to create duplicate typed properties for each (argumentsArray and argumentsFunction, because there is no performance loss when I test it using Grant Skinner's PerformanceTest class, and untyped things like this make a negligible difference).

             

            So what would the compiler do in that case?  I feel like I would have to use the IMXMLObject's "initialized" method to translate the string version of everything into an Array or Function, haven't tried it yet, what do you think?

             

            Thanks,

            Lance

            • 3. Re: MXML, difference between "{object.method()}" and "object.method()"?
              viatropos Level 1

              Nope, doesn't work.  You can kind of solve it by implementing IMXMLObject like this:

               

               

              public function initialized(document:Object, id:String):void {

                   if (this.arguments is String) {

                        try {

                             this.arguments = document[this.arguments];

                        } catch (e:Error) {}

                   }

              }

               

               

              ... but if it's not a "public" function, it will throw an error.  I guess I'll just have to duplicate the properties, one for the desired result, one if the desired result is returned from the function.

               

              Cheers,

              Lance

              • 4. Re: MXML, difference between "{object.method()}" and "object.method()"?
                viatropos Level 1

                Another question/request on this topic:

                 

                If I had a property that was as so:

                 

                public var condition:Boolean;

                 

                and I used it in MXML like so:

                 

                <MethodObject condition="getConditions"/>

                 

                I would like to be able to update that value only when I explicitly ask for the condition value.  How does that work, if getConditions is like:

                 

                private function getConditions():Boolean {...}

                 

                You could only do that through some complex binding currently.

                • 5. Re: MXML, difference between "{object.method()}" and "object.method()"?
                  matt_chotin Level 3

                  You'd probably need to write some layering in your getter/setter logic, eg., have some other property than "condition" that you'd use to actually execute the get call.  MXML binding is a push model for the most part, not pull.