0

ZK 8 Form Binding How to get middle object outside the command method

asked 2016-12-10 09:06:46 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

updated 2016-12-10 09:14:36 +0800

ZK 8 Form Binding How to get object outside the command method

Before ZK 8, we can do as follows

https://www.zkoss.org/wiki/ZKDeveloper'sReference/MVVM/DataBinding/FormBinding#InitializewithForm_Object

delete flag offensive retag edit

4 Answers

Sort by ยป oldest newest most voted
2

answered 2016-12-14 02:51:05 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2016-12-14 03:15:35 +0800

Yes it works a little different compared to ZK 7. (I'd argue it's simpler now. ;) )

I created a running example on zkfiddle (please be sure to select ZK 8.0.3)

http://zkfiddle.org/sample/6s7puk/1-initialize-form-proxy-in-VM

I noticed I had to call ProxyHelper.createFormProxy(...); instead of ProxyHelper.createProxyIfAny(...); (my bad, I mixed it up when answering out of my head - Next time it will be more efficient if you can provide a SIMPLE and running example so I can just fix/add the missing bits)

Besides in your example you forgot to add the @init(vm.pojoForm) in the form binding. If forgotten it will create a new FormProxy and can't use the pre initialized form in your VM.

Also I suggest using @Init instead of @AfterCompose. @Init will be called before the children are created. @AfterCompose will be called after the children are created. In MVVM you usually try to be independen of components so @AfterCompose is necessary less commonly.

I hope this helps. If not please update the fiddle example to show exactly where the problem is.

Robert

link publish delete flag offensive edit

Comments

Yes It works. Actually my code has @init(vm.pojoform), during copy an paste, that is missed here. The Problem is we need to use createFormProxy not createProxyIfAny. Thanks . I will continue my migration effort from ZK7 to ZK8 and let you know if i found any other issues in this topic.

Senthilchettyin ( 2016-12-14 04:09:46 +0800 )edit

thanks that's great to hear

cor3000 ( 2016-12-14 06:52:23 +0800 )edit

Another question, can i check on the VM whether the middle object is dirty or not ? I know this can be done using fxStatus in the zul, but what is the way we can check on the java vm side ?

Senthilchettyin ( 2016-12-14 18:44:41 +0800 )edit

just cast the pojoForm into the From interface: as in this updated example http://zkfiddle.org/sample/6s7puk/3-initialize-form-proxy-in-VM and about your next question: NO the form dirty flag won't reset even if you clear all your input

cor3000 ( 2016-12-15 01:12:05 +0800 )edit

Ok Got it. But this is different question on the same subject. Is any example available using middle object for multiple records in grid (with add, new, delete options). That is list of objects.

Senthilchettyin ( 2016-12-20 15:21:10 +0800 )edit
0

answered 2016-12-13 01:17:51 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2016-12-13 01:22:45 +0800

This was just answered in the paragraph above your question.

http://forum.zkoss.org/question/99821/zk-8-form-object-how-to-get-value/?answer=99825#post-id-99825

I'll add this again here, maybe in got lost in the mixed answer.

If you need to initialize and pre-populate some values in the Form you can manually create a proxy:

Pojo originalPojo;
Pojo pojoForm;

@Init
public void init() {
    //retrieve original Pojo ...
    //create a proxy around the originalPojo
    pojoForm = ProxyHelper.createProxyIfAny(originalPojo); 
    //set a value in the form proxy only
    //does not affect the originalPojo direcly, only after successful saving
    pojoForm.setIcdVersion(123456); 
}

public Pojo getPojoForm() {
  return pojoForm;
}

/*getter setter for originalPojo*/

and use it in the zul file:

<groupbox form="@id('fx') @init(vm.pojoForm) @load(vm.originalPojo)
                @save(vm.originalPojo, before='savePojo') " >

As you can see there is no need for an Object like SimpleForm anymore. You still work with your original class' interface, just behind a proxy.

link publish delete flag offensive edit
0

answered 2016-12-13 16:54:29 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Sorry. It is not working the way which it works previous in ZK 7. Here is the case

I Have some 20 to 30 Fields in my Ui which uses the middle object as follows

 <div class="page-content"form="@id('fx') @load(vm.selectedRecord) @save(vm.selectedRecord,before='saveThis')">

Before calling Savethis, I need the values entered by the user and then do something. So i know the only possible way in ZK 8 is, pass the Form object in the command and do necessary stuffs as follows

onClick="@command('test', form=fx)"


@Command
public void test(@BindingParam("form") Form form) {
    CPTFees userentered= (CPTFees) form;
    //DO SOMETHING user entered values, but not saved in the orginal object, 
}

So whenever necessary, i always need to pass the Form Object from the ZUl to VM Method and inside the VM Command Method, get the values in the middle object and do the necessary stuff.

This works for small screen. But in big screen, it looks all my VM Command method having this Parameter , because i need to do something with the middle object value.

So i asked the question. Is there any way to keep the reference of the middle object always alive in the VM, without depending on each Command Method.

I tried your method as follows, But it does not works for me.

 private CPTFees selectedRecord;
private CPTFees pojoForm; 

@AfterCompose
public void initSetup()
{
pojoForm = ProxyHelper.createProxyIfAny(this.selectedRecord);
}

@Command
public void Testing() {
   // Assume that user already entered the value for the Field name. So the following 
   pojoForm.getName()
   // The above suppose to give the name entered by the user, but it nulls
  //  So the above object is same as original object, then what is the use of this ?
}

onClick="@command('Testing')"
link publish delete flag offensive edit
0

answered 2016-12-16 09:31:10 +0800

Lisa123456 gravatar image Lisa123456
1

It is really cool.here is an amusing thing to enjoy with you.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2016-12-10 09:06:46 +0800

Seen: 75 times

Last updated: Dec 16 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More