Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Clarification in View  (Read 6501 times)
Durairaj
Newbie
*
Posts: 2


View Profile Email
« on: May 16, 2009, 04:10:13 »

Hi Cliff,

I was digging through in the framework classes and i found something which is not needed or may be i didnt get it correctly? Could you please explain the same

In view class ,

public function registerObserver ( notificationName:String, observer:IObserver ) : void
      {
         var observers:Array = observerMap[ notificationName ];
         
         if( observers ) {
            observers.push( observer );
         } else {
            observerMap[ notificationName ] = [ observer ];   
         }
      }


There is no necessity for pushing the observer into observers local scope array. Since it doesnt do any other function. Is this done for code clarity or i got it wrong?

Durai
« Last Edit: May 16, 2009, 04:18:31 by Durairaj » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: May 17, 2009, 08:33:11 »

It is mostly for clarity, yes. The observers variable in this method is merely a reference to the the array that may or may not exist in observerMap[ notificationName ], it isn't a copy of that array. It is defined so that we may easily evaluate whether the new observer needs be pushed onto the existing array, or have one created if this is the first observer for the notification.

It could be written as below, but that would be less clear and I doubt any more efficient:

:
public function registerObserver ( notificationName:String, observer:IObserver ) : void
{         
  if( observerMap[ notificationName ] ) {
      Array(observerMap[ notificationName ]).push( observer );
  } else {
      observerMap[ notificationName ] = [ observer ];   
  }
}

-=Cliff>

Logged
Pages: [1]
Print