Re: Question regarding smartypants

1 view
Skip to first unread message

Josh McDonald

unread,
Feb 18, 2009, 5:26:10 PM2/18/09
to Nishant Kyal, smartyp...@googlegroups.com
Hi Nishant,

I've crossposted the reply to the google group, hope you don't mind! What you're after unfortunately isn't really possible due to the way MXMLC converts MXML to ActionScript and the lifecycle of Flex Components.

When you declare a visual component (a UIComponent subclass) via MXML, the compiler creates an empty variable with a name matching the id in the parent "document" component, as well as a matching entry in the childDescriptors array. Then during createChildren(), it calls createChildrenFromDescriptors() which goes through, creates new instances of the components and then calls addChild(newComponent). Unfortunately there's not a lot of freedom to mess with this process, which at the moment is the price we pay for the nice declarative syntax of MXML.

Another problem of course is that any DisplayObject can only have one parent DisplayObject - so even if we could use the injector to provide the instance, we still have to construct one instance for every DisplayObject parent, and we can't use singletons for visual components.

I haven't tried, but off the top of my head It might be possible in to declare a non-visual instance of a display component like this:

<mx:Button id="emptyButtonField"/>{null}</mx:Button>

But that won't work with any classes that have a [DefaultProperty] annotation, and you would still have to manually request an instance from the injector and call addChild() so it doesn't really give you anything.

In short, the nature of Flex is such that using an injector to create / retrieve visual components as part of an MXML document isn't really possible, which is why I built the <smartypants:RequestInjection/> tag, and the "SmartyPants.injectInto(this)" static shortcut method to make it as easy as possible to get things injected into. In future you will be able to specify field injection annotations as class-level annotations, so you can put your [Inject] metadata into the class-wide <mx:metadata/> tag, but this is still only going to be useful for non-visual intjected objects.

If my explanation has made things less clear, or there's anything else, let me know :)

Cheers,
-Josh

On Wed, Feb 18, 2009 at 9:36 PM, Nishant Kyal wrote:
Hi Josh,

Thanks for smartypants. I'm using it in all my flex dev now :-)

I had a question. Right now i see that the only way to inject objects is using the inject metetag. I however want to inject some of my components as MXML tags and not object declarations . Is there a way to do that?

Here's what I want to do,

<model:DesignModel id="designModel"/>

instead of 

[Inject]
var designModel:DesignModel;
addChild(designModel);

in my flex app. And DesignModel needs to have application scope. 

Any help much much appreciated.

Nishant Kyal



--
"Therefore, send not to know For whom the bell tolls. It tolls for thee."

Josh 'G-Funk' McDonald
  -  jo...@joshmcdonald.info
  -  http://twitter.com/sophistifunk
  -  http://flex.joshmcdonald.info/

Reply all
Reply to author
Forward
0 new messages