Using Angular with ZK"

From Documentation
Line 28: Line 28:
  
  
== Hero class ==
+
== Initialize Heroes ==
 
Since we suppose there are hero data at the server side, we need to create a <tt>Hero.java</tt>.
 
Since we suppose there are hero data at the server side, we need to create a <tt>Hero.java</tt>.
 
<source lang='java' high='8'>
 
<source lang='java' high='8'>
Line 45: Line 45:
 
</source>
 
</source>
 
* Line 8: ZK can convert a JSON object into Java object for you, but the Java class must have a no-argument constructor.
 
* Line 8: ZK can convert a JSON object into Java object for you, but the Java class must have a no-argument constructor.
 +
 +
 +
After that, we create a list of Hero in a ViewModel.
 +
<source lang='java'>
 +
public class HeroEditorVM {
 +
 +
private ArrayList<Hero> heroes = new ArrayList<Hero>();
 +
private static Integer currentIndex = 10;
 +
 +
@Init
 +
public void init() {
 +
heroes.add(new Hero(nextId(), "Mr. Nice"));
 +
heroes.add(new Hero(nextId(), "Narco"));
 +
heroes.add(new Hero(nextId(), "Bombasto"));
 +
heroes.add(new Hero(nextId(), "Celeritas"));
 +
heroes.add(new Hero(nextId(), "Magneta"));
 +
heroes.add(new Hero(nextId(), "RubberMan"));
 +
heroes.add(new Hero(nextId(), "Dynama"));
 +
heroes.add(new Hero(nextId(), "Dr IQ"));
 +
heroes.add(new Hero(nextId(), "Magma"));
 +
heroes.add(new Hero(nextId(), "Tornado"));
 +
}
 +
...
 +
}
 +
</source>
  
 
= Download =  
 
= Download =  

Revision as of 10:54, 22 May 2017

Using Angular with ZK

Author
Hawk Chen, Engineer, Potix Corporation
Date
Version
ZK 8.0

Overview

Angular [1] is a well-known client-side MVW framework. In the previous article,Integrating ZK with AngularJS, we have introduced how to integrate with AngularJS (1.x), but Angular changes a lot since 2.0. So we think that we also need to introduce more about the integration with Angular again.

In this article, I use the example in Angular tutorial, hero editor, as the base and modify it to communicate with a Java ViewModel at the server-side with client command binding.

Load Heroes from the Server

First, we need to rename index.html to index.zhtml to be processed by ZK and keep all existing tag elements unchanged in the file. Then add a ZK <z:div> for applying ViewModel.

<html xmlns:z="zul" xmlns="native">
...
    	<z:div id="heroes"
    		viewModel="@id('vm')@init('org.zkoss.zkangular.HeroEditorVM')">
    		<my-app>Loading...</my-app>
    		<br/><br/>
    		<z:button label="list all at the server" onClick="@command('show')"/>
    	</z:div>


Initialize Heroes

Since we suppose there are hero data at the server side, we need to create a Hero.java.

public class Hero {

	private int id;
	private String name;
	/**
	 * the no-argument constructor is required for converting a JSON into a Java object.
	 */
	public Hero(){

	}
...
}
  • Line 8: ZK can convert a JSON object into Java object for you, but the Java class must have a no-argument constructor.


After that, we create a list of Hero in a ViewModel.

public class HeroEditorVM {

	private ArrayList<Hero> heroes = new ArrayList<Hero>();
	private static Integer currentIndex = 10;

	@Init
	public void init() {
		heroes.add(new Hero(nextId(), "Mr. Nice"));
		heroes.add(new Hero(nextId(), "Narco"));
		heroes.add(new Hero(nextId(), "Bombasto"));
		heroes.add(new Hero(nextId(), "Celeritas"));
		heroes.add(new Hero(nextId(), "Magneta"));
		heroes.add(new Hero(nextId(), "RubberMan"));
		heroes.add(new Hero(nextId(), "Dynama"));
		heroes.add(new Hero(nextId(), "Dr IQ"));
		heroes.add(new Hero(nextId(), "Magma"));
		heroes.add(new Hero(nextId(), "Tornado"));
	}
...
}

Download

  1. According to [http://angularjs.blogspot.tw/2016/12/ok-let-me-explain-its-going-to-be.html Angular naming guideline, we should use "Angular" for versions 2.0.0 and later.


Comments



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