0

[Solved]form binding, how get default value ?

asked 2014-09-25 05:54:36 +0800

unitcell gravatar image unitcell
28 4

updated 2014-09-26 08:58:32 +0800

Hi all,

using form binding, the part of code is below,


View:
 <div form="@id('fx') @load(vm.personModel) @save(vm.personModel, before='save')">
    Name: <textbox value="@load(fx.name)"/>
    Date: <datebox value="@save(fx.date)" onCreate="self.setRawValue(new Date())" format="yyyy/MM/dd"/>
  </div>
  <div> 
  <button label="save" onClick="@command('save')" />

VM:
public class PersonVM {
    private Person person;

    public Person getPersonModel() {

        if(person == null) {
          person = new Person("Water", new Date());
        }
        return person;
    }

    @Command
    public void save() {
      System.out.println(person.getName());
      System.out.println(person.getDate());
    }

}

If user do not anything, just click save button, it will got person.getDate() is null. How do I get default date from view end ?

Thanx.

delete flag offensive retag edit

3 Answers

Sort by ยป oldest newest most voted
0

answered 2014-09-26 04:23:24 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2014-09-26 04:32:01 +0800

In MVVM you want the ViewModel to control the view state, so it is correct to initialize the date in your constructor as you already do.

If you want to load/save the date from the personModel you need to specify

@bind(fx.date) instead of just @save(fx.date)

please see this running example on zk fiddle:

If you wanted to accomplish something different please feel free to update the fiddle and post the new link here so I can have a second look.

Robert

link publish delete flag offensive edit
0

answered 2014-09-26 08:58:03 +0800

unitcell gravatar image unitcell
28 4

Yes, @bind(fx.date) instead of just @save(fx.date) It work. Thanx a lot !

link publish delete flag offensive edit
-1

answered 2014-09-25 12:39:14 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

Please use

@AfterCompose
public void afterCompose(){
//Your logic of initialization of value 
}

It will initialize zul page with default value

link publish delete flag offensive edit

Comments

I'd prefer @Init to @AfterCompose which is used quite rarely and very likely not necessary in this case, it is often used in hybrid situations when MVVM and MVC principles are mixed and the VM is depending on component creation (I usually try to avoid such situations)

cor3000 ( 2014-09-26 04:30:08 +0800 )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
1 follower

RSS

Stats

Asked: 2014-09-25 05:54:36 +0800

Seen: 42 times

Last updated: Sep 26 '14

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