Communication between ViewModel and Composer"

From Documentation
(Created page with "{{ZKDevelopersReferencePageHeader}} Communication between a composer and a ViewModel is done by '''Global Command'''. =Posting a Command from a Composer to a ViewModel = Compo...")
 
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
 +
 +
{{Template:UnderConstruction}}
 +
  
 
Communication between a composer and a ViewModel is done by '''Global Command'''.
 
Communication between a composer and a ViewModel is done by '''Global Command'''.

Revision as of 04:28, 17 May 2012


DocumentationZK Developer's ReferenceMVVMAdvancedCommunication between ViewModel and Composer
Communication between ViewModel and Composer


WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!


Communication between a composer and a ViewModel is done by Global Command.

Posting a Command from a Composer to a ViewModel

Composer doesn't have a Binder to send a global command for it, To offer communication there needs to be a way for the MVC pattern bound section of the application to pass a command to the binder which will in turn talk to all the view models and tell them to run a particular command. This is achieved by posting a global command and works in a much similar way to the normal command system.

The example below shows a function which was taken from our SelectorComposer (MVC pattern) and shows the issuing of a global command. In the product view upon clicking of the add button will result in the execution of said function.

@Listen("onAddProductOrder=#PrdoDiv #prodGrid row productOrder")
public void addProduct(Event fe) {

	if (!(fe.getTarget() instanceof ProductOrder)) {
		return;
	}

	ProductOrder po = (ProductOrder) fe.getTarget();

	try {
		UserUtils.getShoppingCart()
			.add(po.getProduct(), po.getQuantity());
	} catch (OverQuantityException e) {
		po.setError(e.getMessage());
	}

	BindUtils.postGlobalCommand(null, null, "updateShoppingCart", null);
}

Line 17 shows the function "BindUtils" and its function "postGlobalCommand" is used to post a command to the binder. In this case the first two strings take the name of the binder and the scope of said binder, in this case it is set to null to use the default. In most cases one would want to set this to null. Then the string name for the command is given along with a map of arguments to pass, in this case null as there are no arguments.

This globalCommand is then executed by the binder on any view model that has registered for it. In the case of this application the ShoppingCartViewModel needs to refresh the cart items when a product is added to a cart. Therefore a function is created in the ShoppingCartViewModel and registered as a global command, this function has the ability to do some processing and then notify the binder that the cartItems in that view have changed. The snippet below shows this in action.

public class ShoppingCartViewModel {
	
	...
	
	@GlobalCommand
	@NotifyChange("cartItems")
	public void updateShoppingCart() {
		//no post processing to be done
	}
}

This takes care of passing commmands from the MVC composer to MVVM view model, the next section discusses message passing between MVVM view models.


Posting a Command from a ViewModel to a Composer

Version History

Last Update : 2012/05/17


Version Date Content
6.0.0 May 2012 Supplement to advanced topic.




Last Update : 2012/05/17

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.