nwebb

Flex, Flash, AIR

A Few Swiz Tips For Beginners

Swiz Beginner's FAQ

When I started to look in to Swiz for the first time the other day, I had a few questions; Some answers were found in the official FAQ, and others elsewhere (mailing-group, web searches etc). I thought I'd collate what I found most useful, in to an easy to digest post that would make handy reading before starting your first Swiz project.

Remember that the docs are being updated all the time, and the alpha of Swiz 1.0 is already out, so check the official site for the most up-to-date info relating to the version of Swiz you are using. However I'd guess that most stuff here will remain fairly relevant for some time to come. I'm also likely to update this post when I come across/remember anything else, so check back.


Q & A:

Q: It looks as if I could end up with a lot of classes being instantiated in my BeanLoader(s). Wouldn't that have a negative impact on start-up performance?

A: Look at using the Prototype class, which among other things offers deferred instantiation. Prototype beans will not be created until they are needed for injection.

Q:[Mediate] looks a lot like the equivalent of addEventListener(). Is there an equivalent of removeEventListener()?

A: "No. [Mediate] is generally used in non-view classes like controllers, so we didn't account for that use case (weak listeners / removing mediate). Views should rarely be listening for 'system events' (and I think that is even more the case with views that are added and removed during app lifecycle*). Your views should generally just have a presentation model injected into them, or be managed by a mediator, or work in some other mostly passive fashion."

From Ben Clinkinbeard via GoogleGroups archive.
* relates to the specific question being asked by the user.

NOTE: You can still use [Mediate] within a view. Take this example where the MainView class (which encompasses all the app's views) listens for application-wide alerts and displays them via the Alert popup. This 'listener' does not need to be removed during the life of the application so it doesn't seem to pose much of an issue (although there would still be better places to put this code, such as in the presentation model if you are using one).

//From Richard Lord's Swiz "Flexcast" example (http://www.richardlord.net/blog/flexcaster-swiz)
//This "listener" doesn't need to be removed during the life of the application.
[Mediate( event="AlertEvent.SHOW_ALERT" )]
public function alertEventHandler( event : AlertEvent ) : void
{
     Alert.show( event.message, event.title );
}

Q: Where do I start implementing my logic?

A:  Let your main controller implement the interface IInitializingBean and start your application logic in the initialize() method. Swiz calls initialize() on these beans when the autowiring process is complete.

e.g. public class ApplicationController extends AbstractController implements IInitializingBean
NOTE : Swiz autowires the view on addedToStage which is after creationComplete.

Q: I've looked at someone else's Swiz project & I cannot see where [x] is being done. Am I going mad?

A: Possibly! ... but it's also possible to initialise a class in the BeanLoader and not refer to it elsewhere in the application, yet still have it react to mediated events. I've often seen things like an app's main controller instantiated and used like this in demo applications. So, remember to check the BeanLoader for classes that are only referred to / initialised in that one place.

Q: I am injecting a property in to my view/presentation-model, but when I try to access that property it is still null. How can I be notified of when that property is set?

A:  Instead of injecting in to a property, use a getter/setter instead. See the example below.

Actionscript:
  1. //THE FIRST WAY (but how do you know when 'locations' has been set?)
  2. //[Bindable]
  3. //[Autowire(bean="appModel", property="locations")]
  4. //public var locations:ArrayCollection;
  5.  
  6. //THE 'IMPROVED' WAY ;)
  7. [Bindable]
  8. [Autowire(bean="appModel", property="locations")]
  9. public function get locations():ArrayCollection
  10. {
  11.     return _locations;
  12. }
  13. public function set locations(value:ArrayCollection):void
  14. {
  15.     _locations = value;
  16.        
  17.         //let other parts of my view react to that fact that locations is now set. For example ...
  18.     dispatchEvent (new Event("locationsLoadedFromDB") ); //dispatching a custom event-type
  19. }
  20.  
  21. //AND IN THE SAME CLASS ....
  22. // The custom property dispatched from the setter will trigger this binding, and therefore anything bound to 'nextBtnEnabled' will update.
  23. [Bindable("locationsLoadedFromDB")]
  24. public function get nextBtnEnabled():Boolean
  25. {
  26.     if(locations) return (locations.length> 0);
  27.     return false;
  28. }

No comments

No comments yet. Be the first.

Leave a reply

Bad Behavior has blocked 517 access attempts in the last 7 days.