0

MVVM Form Binding

asked 2013-04-10 14:04:56 +0800

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

As per this book reference, it shows how we can show some notification whenever user changes the data using form status variable (dirty or not).

But my requirement not to show any image, instead of that, on click of close button of the window, i need to ask the confirmation to the user.

So can we get this value in the view model java class ?

delete flag offensive retag edit

Comments

Just so we're clear, do you just need to know when a regular window closes? and then have the corresponding VM notified? or is it a popup window? I think there are some security restrictions if its the main browser window, but someone else can confirm. A popup window you'll have more control of.

rickcr ( 2013-04-10 23:41:36 +0800 )edit

You could also try to listen (javascript) for onbeforeunload and then fire a command to your ViewModel from Javascript? This latter, I have not tried and would like to find the documentation on how to call a ViewModel command from javascript. Closest I found was this http://tinyurl.com/cayfuml

rickcr ( 2013-04-10 23:47:17 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2013-04-11 01:34:27 +0800

Matze2 gravatar image Matze2
773 7

Hope I understood correct the question. If you create the form yourself in the view model then you have programmatic access to dirty state easily. This is described at the end of your book reference, section "Initialize with Form Object".

link publish delete flag offensive edit

Comments

Yes Matze. You are right. But i am looking for example using our own middle object.:).

Senthilchettyin ( 2013-04-11 03:11:12 +0800 )edit

I use an extension of SimpleForm where setField() method is overridden. In your setField you can monitor all changes to the middle object.

Matze2 ( 2013-04-11 07:48:36 +0800 )edit

Can you please post the code to understand better ?

Senthilchettyin ( 2013-04-12 11:45:32 +0800 )edit

Just have a look in ZK-1048. There I posted the Form extension I usually use.

Matze2 ( 2013-04-12 15:51:12 +0800 )edit
0

answered 2013-04-11 12:52:33 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

You can use zk.beforeUnload to send an ajax request by jq.ajax as an au request to trigger command in mvvm.

Regarding how to send au request by jq.ajax function, please refer to _rmDesktop in au.js

au.js#_rmDesktop

Regarding what data should be put in request data, you can follow the article provided by rickcr above to send a normal au request and see form data in browser console:

How to call java method using javascript in ZK MVVM?

the form data should similar to the screen shot below:

au form data

for more information, please refer to the full sample below:

test.zul

<zk>
    <script><![CDATA[
        zk.afterMount(function () {
            // do something before unload
            zk.beforeUnload(function() {
                // is dirty
                if (zk.Widget.$('$cbx')._checked) {
                    var wgt = zk.Widget.$('$div');
                    /* if you do not sure what should be put in the data,
                        uncomment this line below and comment jq.ajax(...) section then
                        you can see the data in browser console
                    zAu.send(new zk.Event(zk.Widget.$('$div'), "onUnloadWithDirty", {}, {toServer:true}));
                    */
                    // send event to component immediately
                    jq.ajax(zk.$default({
                        url: zk.ajaxURI(null, {desktop:wgt.desktop,au:true}),
                        data: {dtid: wgt.desktop.id, cmd_0: 'onUnloadWithDirty', uuid_0: wgt.uuid, data_0: {}},
                        async: !!zk.ie
                    }, zAu.ajaxSettings), null, true);
                }
                return 'Data auto saved, Leave page ?';
            })
        });
    ]]></script>
    <div id="div" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('test.TestVM')"
        onUnloadWithDirty="@command('autoSave')">
    <groupbox form="@id('fx') @load(vm.data) @save(vm.data, before='saveOrder')" >
        <textbox value="@bind(fx.name)" />
        <textbox value="@bind(fx.value)" />
        <checkbox id="cbx" label="dirty?" checked="@bind(fxStatus.dirty)" />    
    </groupbox>
    </div>
</zk>

TestVM.java

package test;

import org.zkoss.bind.annotation.Command;

public class TestVM {
    public Data getData () {
        return new Data("name of date", "value of data");
    }
    public static class Data {
        private String _name;
        private String _value;
        public Data (String name, String value) {
            _name = name;
            _value = value;
        }
        public String getName () {
            return _name;
        }
        public String getValue () {
            return _value;
        }
    }
    @Command
    public void saveOrder () {

    }
    @Command
    public void autoSave () {
        System.out.println(" do auto save");
    }
}
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: 2013-04-10 14:04:56 +0800

Seen: 130 times

Last updated: Apr 11 '13

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