views:

36

answers:

2

If I wanted to do something like this:

<mx:Canvas  xmlns:mx="http://www.adobe.com/2006/mxml" 
            horizontalScrollPolicy="off" 
            verticalScrollPolicy="off"  
            xmlns:view="com.foo.bar.view.*" 
>
    <mx:Script>
      <![CDATA[
        myWidth = 100;
        myHeight = 200;
        myCanvas.width = myWidth;
        myCanvas.height = myHeight;
      ]]>
    </mx:Script>
</mx:Canvas>

How would I get a handle on myCanvas (where I'd want myCanvas to be the root )?

+1  A: 

You don't need to if you make myWidth and myHeight bindable, and set width="{myWidth}" and height="{myHeight}" in the Canvas declaration.

<mx:Canvas  xmlns:mx="http://www.adobe.com/2006/mxml" 
        horizontalScrollPolicy="off" 
        verticalScrollPolicy="off"  
        width="{myWidth}"
        height="{myHeight}"
        xmlns:view="com.foo.bar.view.*" >
  <mx:Script>
  <![CDATA[
     [Bindable]
     private var myWidth:Number;
     [Bindable]
     private var myHeight:Number;
  ]]>
  </mx:Script>
</mx:Canvas>

Any changes to myWidth and myHeight would then update the size of the Canvas

rlovtang
Hmm, that's an interesting idea. I never thought of just binding it to something I could get a handle on...
Aaron B. Russell
Okay, that definitely seems to do what I'm after, though I have one catch: I need to get at those variables from outside this MXML file -- this is a Cairngorm 2.x project, so I need my Command classes to be able to edit the height/width of this canvas, but I'm seeing this compiler warning: Warning: var 'canvasWidth' will be scoped to the default namespace: ProjectName: internal. It will not be visible outside of this package. var canvasWidth = model.somethingWidth;
Aaron B. Russell
What if you create a public setter for myWidth and myHeight?
rlovtang
You should probably not need to create your own variables and bind to them, but rather use Amarghosh's solution. But if you need to bind the width and hight to some deeper nested variables or a function, then binding is a good option.
rlovtang
+3  A: 

To access the component specified by the root node from within an mxml file, you can use this keyword. Any code inside an mxml runs in the context of this object - you can as well omit the keyword if you don't have any local variable by the same name.

this.width = myWidth;
this.height = myHeight;

For your second question:

Let's say your mxml file's name is MyCanvas.mxml. You'd add this to another component using <ns:MyCanvas/> tag. You can set an id there and access it using that.

<ns:MyCanvas id="myCanvas"/>

Inside script:

myCanvas.width = whatever;
Amarghosh
This makes sense, however whenever I call myCanvas.width, I'm getting "Error: Access of undefined property myCanvas" -- looks like I'm missing an import somewhere, but I'm importing com.foo.bar.view.* -- do I need to somehow import main.mxml from my ActionScript code to get at the instance of myCanvas?
Aaron B. Russell
@Aaron make sure that you're spelling it correct. Also, remember that `myCanvas.width` is only for accessing it from another file. Within the same file, use `this.width` or just `width`.
Amarghosh
Ah, looks like I needed to add a `var myCanvas : MyCanvas;` line. Thanks everyone for your help. :)
Aaron B. Russell