It's not the application that you want to `with` in this case, it's the instance.

But, we want to avoid the use of `with`, because it is inefficient (implies runtime lookup), so the right thing here is to have the tag compiler add these anonymous functions as methods (so they implicitly inherit the class and instance scope), since that's what they really are -- they are invoked as methods, it's just that since they were not defined as methods they don't have `this` in their scope. It does mean we need to come up with a scheme for naming them that will not collide with superclasses or user methods. Namespaces to the rescue? Will it be sufficient to name these methods `$lzsc::$m<n>`?

Eventually we could be more efficient still: JS2 does not need delegates, because methods will correctly close over instances. So rather than:

  ... = new LzDelegate(<instance>, '<method>')

you can just say:

  ... = <instance>.<method>

which will give you a closure, which when invoked will have `this` bound to <instance>. We should create a task for this. If there were some way to detect this case in the JS1 compiler and hand-create the equivalent closure, we could have a big win in all runtimes.

On 2008-01-10, at 07:25 EST, Donald Anderson wrote:

Once upon a time, I experimented with adding 'with (lzApplicationInstance)' if I saw a 'withThis' pragma. (lzApplication is a variable that points to
the application instance).  Not sure that is what is needed here...?

- Don

On Jan 9, 2008, at 11:23 PM, P T Withington wrote:

Well I thought our handlers were supposed to be compiled with 'implicit this'.

Perhaps Don left this out, because this _is_ implicit in AS3 method bodies? If so, we should make the tag compiler output anonymous handlers like this as methods not functions. This would mean we would have to start emitting those anonymous singleton classes for views that add methods...

On 2008-01-09, at 23:06 EST, Henry Minsky wrote:

I've got a little test LZX code that has a click handler like this:

<view id="bar" x="200" y="200" >
 <view id="foo" bgcolor="0xcccccc" x="-100" y="-100"  height="200"
width="200"
       onclick="parent.animate('rotation', 360, 1500, true)"


When I compile it I get this error from the  flex compiler

/cygdrive/c/flex2/bin/mxmlc -debug=true
-library-path+=../../../lps/includes/lfc/LFC9.lzl -default-size 800 600
-default-frame-rate 60 DemoApp.as
Loading configuration file C:\flex2\frameworks\flex-config.xml
C:\users\hqm\openlaszlo\devildog\WEB-INF\lps\lfc\DemoApp.as(49): col: 20 Error: Call to a possibly undefined method animate through a reference with
static type flash.display:DisplayObjectContainer.

         parent.animate('rotation', 360, 1500, true)
                ^

The handler is compiled to a function inside of the usual stuff passed to a
call to LzInstiateView:


LzInstantiateView({attrs: {id: "bar",
                        x: 200,
                        y: 200},
                children: [{attrs:
                            {$delegates: ["onclick", "$m1", null,
"oninit", "$m2_foo_reference", null],
                             $m1: function foo_$m1() {
                     parent.animate('rotation', 360, 1500, true)
                 }, ...


So I am not sure in what context the compiler thinks that "parent" is being
looked up.


If I change the code to refer to "this.parent", it compiles happily, and
appears to work

<view id="bar" x="200" y="200" >
 <view id="foo" bgcolor="0xcccccc" x="-100" y="-100"  height="200"
width="200"
       onclick="this.parent.animate('rotation', 360, 1500, true)"




--
Henry Minsky
Software Architect
[EMAIL PROTECTED]



--

Don Anderson
Java/C/C++, Berkeley DB, systems consultant

voice: 617-547-7881
email: [EMAIL PROTECTED]
www: http://www.ddanderson.com





Reply via email to