开发者

Access objects instantiated in Flex app's MXML file in other AS classes

I've got an object declared and instantiated in my Flex application's singular MXML file:

public var CDN:CDNClass = new CDNClass;

I would like to access this same CDN object (and its public methods and properties) in another class declared in a separate .as file as such:

package my.vp
{
    import my.media.CDNClass;

    public class SyncConnectorManager
    {

        private function syncMessageReceived(p_evt:SyncSwfEvent):void
        {
            switch (p_evt.data.msgNm)
            {
     开发者_运维知识库           case "startStream" :
                    // Play a stream
                    CDN.parsePlayList(p_evt.data.msgVal);
                    break;

But when I try to access the public method parsePlayList in the CDN object in a method in the class defined in the .as file, I get the following error:

Access of undefined property CDN

The reason I want to do this is to break up the logic of my application into multiple AS files and have minimal MXML files, probably only one.

Thanks - any help is much appreciated. Perhaps my OOD/OOP thinking is not correct here?


IT depends on your class architecture. For your code to work, the CDNClass instance must be defined and implemented inside your SyncConnectorManager.

Generally, you can always call down into components, but should never call up

One option is to pass the instance ofCDNClass to a variable inside SyncConnectorManager. Add this variable to your SyncConnectionManager class:

public var CDN:CDNClass = new CDNClass;

And at some point do this:

syncConnectorManagerInstance.CDN = CDN;

That way both classes will have access to the same CDN instance and can call methods on it.


Yes, your OOP thinking is not correct here. You should take in mind differences between classes and instances. This line declares a filed in a current class and initiates it with an instance:

public var CDN:CDNClass = new CDNClass;

So current instance of your MXML class (you can think about it as usual AS class with some other notation) has public field. To operate with CDN instance you need something from the following:

  • Read the value of CDN (as far as it is public) from the instance of your MXML class. You need some reference to it for that.
  • The instance of your MXML class can have a reference to the instance of SyncConnectorManager and SyncConnectorManager should have a way to inject the value of CDN there. Something like:

Your class:

package my.vp
{
    import my.media.CDNClass;

    public class SyncConnectorManager
    {
        private var CDN:CDNClass;

        public function SyncConnectorManager(CDN:CDNClass)
        {
             this.CDN = CDN;
        }

        private function syncMessageReceived(p_evt:SyncSwfEvent):void
        {
            switch (p_evt.data.msgNm)
            {
                case "startStream" :
                    // Play a stream
                    CDN.parsePlayList(p_evt.data.msgVal);
                    break;

In your case SyncConnectorManager class hasn't CDN declared (the problem of the compiler error you mentioned) and instantiated (the problem of NPE even if you just declare field).

As the bottom line I can suggest you to follow ActionScript naming and coding conventions to talk other people and team members about your code :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜