Initialization"

From Documentation
m ((via JWB))
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
 
+
{{Deprecated | url=[http://books.zkoss.org/zk-mvvm-book/8.0/viewmodel/initialization.html zk-mvvm-book/8.0/viewmodel/initialization]|}}
  
 
== Initial Method ==
 
== Initial Method ==
  
The binder is responsible for creating and initializing a ViewModel instance. If you want to perform your own initialization in a ViewModel, you can declare your own '''initial method''' by annotating a method with <tt> @Init </tt>. The Binder will invoke this method when initializing a ViewModel during a page creation phase. Each ViewModel can only have one initial method.
+
The binder is responsible for creating and initializing a ViewModel instance. If you want to perform your own initialization in a ViewModel, you can declare your own '''initial method''' by annotating a method with <code>@Init</code>. The Binder will invoke this method when initializing a ViewModel during a page creation phase. Each ViewModel can only have one initial method.
  
 
<source lang="java">
 
<source lang="java">
Line 19: Line 19:
 
This annotation has an attribute named "superclass", if you set it to "true". The binder will look for the initial method of ViewModel's parent class and invoke it first if it exists.
 
This annotation has an attribute named "superclass", if you set it to "true". The binder will look for the initial method of ViewModel's parent class and invoke it first if it exists.
  
<source lang="java" high='12'>
+
<source lang="java" highlight='12'>
  
 
public class ParentViewModel{
 
public class ParentViewModel{
Line 38: Line 38:
 
}
 
}
 
</source>
 
</source>
* Line 12: <tt>ParentViewModel</tt>'s initial method is invoked first then <tt>ChildViewModel</tt>'s.
+
* Line 12: <code>ParentViewModel</code>'s initial method is invoked first then <code>ChildViewModel</code>'s.
  
 
Please notice that child class's initial method '''should not override parent class's initial method''', or parent's initial method won't be called and child's will be called twice.
 
Please notice that child class's initial method '''should not override parent class's initial method''', or parent's initial method won't be called and child's will be called twice.
Line 46: Line 46:
 
   Since 6.0.1
 
   Since 6.0.1
  
If super has an init methold but its ChildViewModel doesn't, you can add <tt> @Init(superclass=true) </tt> on the ChildViewModel to use super's init.
+
If super has an init methold but its ChildViewModel doesn't, you can add <code>@Init(superclass=true)</code> on the ChildViewModel to use super's init.
<source lang="java" high="6">
+
<source lang="java" highlight="6">
 
public class ParentViewModel {
 
public class ParentViewModel {
 
     @Init
 
     @Init
Line 61: Line 61:
  
 
=Version History=
 
=Version History=
{{LastUpdated}}
+
 
{| border='1px' | width="100%"
+
{| class='wikitable' | width="100%"
 
! Version !! Date !! Content
 
! Version !! Date !! Content
 
|-
 
|-

Latest revision as of 07:36, 8 July 2022

Stop.png This article is out of date, please refer to zk-mvvm-book/8.0/viewmodel/initialization for more up to date information.

Initial Method

The binder is responsible for creating and initializing a ViewModel instance. If you want to perform your own initialization in a ViewModel, you can declare your own initial method by annotating a method with @Init. The Binder will invoke this method when initializing a ViewModel during a page creation phase. Each ViewModel can only have one initial method.

public class MyViewModel {

	@Init
	public void init(){
		//initialization code
	}
}

This annotation has an attribute named "superclass", if you set it to "true". The binder will look for the initial method of ViewModel's parent class and invoke it first if it exists.

public class ParentViewModel{

	@Init
	public void parentInit(){
		//initialization code
	}
}


public class ChildViewModel extends ParentViewModel{

	@Init(superclass=true)
	public void childInit(){
		//initialization code
	}
}
  • Line 12: ParentViewModel's initial method is invoked first then ChildViewModel's.

Please notice that child class's initial method should not override parent class's initial method, or parent's initial method won't be called and child's will be called twice.

Apply on Class

 Since 6.0.1

If super has an init methold but its ChildViewModel doesn't, you can add @Init(superclass=true) on the ChildViewModel to use super's init.

public class ParentViewModel {
    @Init
    public void init(){}
}

@Init(superclass=true)
public class ChildViewModel extends ParentViewModel{
}

The initial method can retrieve various context object by applying annotation on its parameter, please refer ZK Developer's Reference/MVVM/Advance/Parameters.

Version History

Version Date Content
6.0.0 February 2012 The MVVM was introduced.



Last Update : 2022/07/08

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