Swiz eventPackages in strict mode

I have added a new extension to Swiz when used in strict mode. You can now define event packages to have a shortened notation for mediating events.

To dynamically mediate an event you define a mediate annotion typically in a controller class which has to be defined in a BeanLoader. Let’s assume our project has the package com.soenkerohde.example and our events are in com.soenkerohde.example.event.

In your main MXML in the preinitialize event handler Swiz should be initialized like this:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	preinitialize="pi()">
	<mx:Script>
		<![CDATA[
			import com.soenkerohde.example.Beans;
			import org.swizframework.Swiz;
 
			private function pi():void
			{
				Swiz.setStrict(true, "com.soenkerohde.example.event").loadBeans([Beans]);
			}
 
		]]>
	</mx:Script>
 
</mx:WindowedApplication>

You see the new second parameter in the setStrict method call which defines the package where our event classes live. If we want to mediate let’s say the LoginEvent your controller class (which must be loaded in a BeanLoader) previously looked like this:

[Mediate(event="com.soenkerohde.example.event.LoginEvent.LOGIN", properties="username,password")]
public function loginHandler(username:String, password:String):...

With the new event packages feature you can remove the event package like this:

[Mediate(event="LoginEvent.LOGIN", properties="username,password")]
public function loginHandler(username:String, password:String):...

The LoginEvent class:

package com.soenkerohde.example.event
{
	import flash.events.Event;
 
	public class LoginEvent extends Event
	{
		public static const LOGIN:String = "LoginEventConstantValue";
 
		public var username:String;
		public var password:String;
 
		public function LoginEvent(type:String, username:String, password:String)
		{
			super(type);
			this.username = username;
			this.password = password;
		}
 
	}
}

The LOGIN constant contains the type of the event so when you dispatch the LoginEvent it will be like this:

Swiz.dispatchEvent(new LoginEvent(LoginEvent.LOGIN, "foo", "bar"));

The string value of the event type has to be unique for your application. Because what happens behind the scenes is that Swiz first checks if the class LoginEvent with the constant LOGIN exists. If this is not the case you get a log error message with the event template.
However, if the class and constant exist Swiz evaluates the value of the constant which in this example is: “LoginEventConstantValue”.

So we could also define the mediate like this:

[Mediate(event="LoginEventConstantValue", properties="username,password")]
public function loginHandler(username:String, password:String):...

We would have the same result but no runtime validation that our event class exists. And because at the end only the value of the event constant counts be sure that it is a unique. To unsure this you could make use of the UIDUtil from the mx.utils package:

public static const LOGIN:String = UIDUtil.createUID();

If you have multiple packages containing events (e.g. with modules) you can add them with:

Swiz.getInstance().addEventPackage("com.foo.event")

or also while initializing:

Swiz.setStrict(true, "com.soenkerohde.example.event").addEventPackage("com.foo.event").loadBeans([Beans]);

If the event class is only in a sub-directory like com.soenkerohde.example.user.LoginEvent only add the user package:

[Mediate(event="user.LoginEvent.LOGIN", properties="username,password")]
public function loginHandler(username:String, password:String):...

The rule with setStrict set to true is that every event with a “.” in the name is considered to be a reference to a class and its event type constant.
So you can still use “weak” event mediators like:

[Mediate(event="initApp")]
public function initHandler():...

which can be invoked with:

Swiz.dispatch("initApp")

However I strongly encourage to always use the strict mode to ensure you haven’t misconfigured anything.

1 comment to Swiz eventPackages in strict mode