📜 ⬆️ ⬇️

Swiz Framework (simplest application)

My previous post: Swiz Framework (short review)

In this post I will try to explain what it consists of and how the simplest application written on the Swiz framework works. If you have any questions or comments, write comments, I will try to answer everyone. Sources that are described in this example can be found here.

So the simplest diagram of how a Flex application works using Swiz is as follows:
The View (or Presentation model) dispatch event, which is caught by the Controller with the help of the [Mediate(event="someEvent")] meta tag, then the controller calls the service and changes the model. The view only reflects changes in the model.


In order to understand what is being injected, you can look at the scheme:
The model is injected into the view and controller. Appropriately the view displays the model, the controller updates it. Services are also injected into the delegate, and the delegate, in turn, is injected into the controller.


The main SwizQuickstart.mxml application includes the SwizQuickstart.mxml component, which specifies beanProviders and config, as well as the UserForm view
 <fx: Declarations>
	 <swiz: Swiz>	
		 <swiz: beanProviders>
			 <config: Beans />
		 </ swiz: beanProviders>
		 <swiz: config>
			 <swiz: SwizConfig 
				 eventPackages = "org.swizframework.quickswiz.event. *" 
				 viewPackages = "org.swizframework.quickswiz.view. *" />
		 </ swiz: config>
	 </ swiz: Swiz>
 </ fx: Declarations>

 <view: UserForm id = "userForm" />

Let's sort everything in order


This class configures the entire framework. It looks like this:
 <swiz: SwizConfig
	 setUpEventType = "{Event.ADDED_TO_STAGE}" 
	 setUpEventPhase = "{EventPhase.CAPTURING_PHASE}" 
	 setUpEventPriority = "50"
	 tearDownEventType = "{Event.REMOVED_FROM_STAGE}" 
	 tearDownEventPhase = "{EventPhase.CAPTURING_PHASE}" 
	 tearDownEventPriority = "50"
	 eventPackages = "com.foo.events, org.bar.events"
	 validateEventTypes = "true"
	 viewPackages = "com.foo.views, org.bar.events"
	 defaultFaultHandler = "handleUnhandledFaults"
	 defaultDispatcher = "global" />


In beanProviders there are controllers, models, services, delegates that need to be included. In the course of processing these classes, they contain all meta tags and all internal processes occur. The <config:Beans /> class looks like this:
 <swiz: BeanProvider 
	 xmlns: fx = "http://ns.adobe.com/mxml/2009"
	 xmlns: swiz = "http://swiz.swizframework.org"
	 xmlns: service = "org.swizframework.quickswiz.service. *" 
	 xmlns: controller = "org.swizframework.quickswiz.controller. *">
	 <service: UserService id = "userService" />
	 <controller: UserController id = "userController" />
	 <! - We'll use the server-side call to help simulate a Swiz ServiceHelper.  ->
	 <swiz: ServiceHelper id = "serviceHelper" />

 </ swiz: BeanProvider>

Appropriate after processing, Beans Swiz already knows that we have services and controllers.


It is a simple mxml, the most interesting thing there is injecting:
 [Inject (source = "userController.currentUser", bind = "true")]
 public var user: User;

Further, in any places, you can use our user model, for example, to display:
  <s: TextInput id = "firstName" text = "{user.firstName}" /> 

How it all works:

View displays what he got from the model.
<s:TextInput id="firstName" text="{user.firstName}" />

Further the user corrects any data, and presses the save button.
<s:Button label="Save" click="saveUser()" />

Dispatch event
user.firstName = firstName.text;
user.lastName = lastName.text;
user.email = email.text;
var event : UserEvent = new UserEvent( UserEvent.SAVE_USER_REQUESTED );
event.user = user;
dispatchEvent( event );

The event is caught in the controller, and calls the service. The userService.saveUser (user) command returns an AsyncToken. This is suitably any delegate and any call that returns an AsyncToken.
 [Mediate (event = "UserEvent.SAVE_USER_REQUESTED", properties = "user")]
 public function saveUser (user: User): void
	 serviceHelper.executeServiceCall (userService.saveUser (user), handleSaveUserResult);

After the AsyncToken otdopatchit event event executes the function handleSaveUserResult
 private function handleSaveUserResult (event: ResultEvent): void
	 Alert.show ('User saved successfully!');

At this point, we could appropriately change the model or otdopatchit any event that could also be successfully caught with the help of the [Mediate] tag in any controller or presentation model.

In the next posts I will look at how a more comprehensive Swiz-based application works, and also write in detail the properties of the Swiz classes.

Source: https://habr.com/ru/post/100492/

All Articles