FlexJS XML problem

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

FlexJS XML problem

Harbs
Given the following XML in Flash:
var xml:XML = <root/>;
xml.bar.foo.baz = "baz";

You get an XML structure like so:
<root>
  <bar>
    <foo>
      <baz>
        baz
      </baz>
    </foo>
  </bar>
</root>

In JS, this compiles to the following:

  var /** @type {XML} */ xml = new XML( '<root/>');
  xml.child('bar').child('foo').setChild('baz', "baz");

.child() returns an empty XMLList
.setChild() tries to add a child to that empty list and does not create the structure.

I’m not sure how to best solve this problem.

One idea would be to add a new function called getChild() which would return an existing element if it exists and create a new one if not. Then we could have the compiler rewrite the output to:

  xml.getChild('bar').getChild('foo').setChild('baz', "baz");

I’d prefer to try and figure out how to have setChild walk up the reference tree and create the correct elements automatically. I’m having trouble figuring out how to do that though.

Harbs
Reply | Threaded
Open this post in threaded view
|

Re: FlexJS XML problem

Harbs
I figured it out and committed a fix.

> On Aug 28, 2017, at 12:07 PM, Harbs <[hidden email]> wrote:
>
> Given the following XML in Flash:
> var xml:XML = <root/>;
> xml.bar.foo.baz = "baz";
>
> You get an XML structure like so:
> <root>
>  <bar>
>    <foo>
>      <baz>
>        baz
>      </baz>
>    </foo>
>  </bar>
> </root>
>
> In JS, this compiles to the following:
>
>  var /** @type {XML} */ xml = new XML( '<root/>');
>  xml.child('bar').child('foo').setChild('baz', "baz");
>
> .child() returns an empty XMLList
> .setChild() tries to add a child to that empty list and does not create the structure.
>
> I’m not sure how to best solve this problem.
>
> One idea would be to add a new function called getChild() which would return an existing element if it exists and create a new one if not. Then we could have the compiler rewrite the output to:
>
>  xml.getChild('bar').getChild('foo').setChild('baz', "baz");
>
> I’d prefer to try and figure out how to have setChild walk up the reference tree and create the correct elements automatically. I’m having trouble figuring out how to do that though.
>
> Harbs