0

Passing parameters from a ViewModel to another one

asked 2018-03-09 21:46:59 +0800

psinalberth gravatar image psinalberth
52 6

Hi,

I have a NavigationViewModel which is responsible for redirect and show the content of my application, and that is my include:

<div class="container is-fluid" style="padding-top: 3.25rem;">
      <div class="columns">
        <include self="@define(content)" viewModel="@id('vm') @init('NavigationViewModel')" src="@load(vm.content)" />
      </div>
    </div>

And this is the navigation method:

    @GlobalCommand
    @NotifyChange("content")
    public void goTo(@BindingParam("content") String content) {
        if (content != null) this.content = content;
    }

So, going to Person page would be something like

@global-command('goTo', content='/person/index.zul')

Since I have index.zul and form.zul, and different ViewModel for each one of them, is it possible to create a method or include a parameter to pass the id of selected person from index.zul to form.zul? I've tried by using @BindingParam("id") in goTo method, but it didn't work. I need to pass a param from a viewmodel to another one.

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-03-13 16:15:33 +0800

cor3000 gravatar image cor3000
4293 1 7
ZK Team

updated 2018-03-13 16:22:42 +0800

I hope this runnable example explains how this can be done: http://zkfiddle.org/sample/3971936/4-Global-Command-Navigation-with-Params

The non obvious part is the "specialty" about <include> that it is only reloading when the url changes ... you might start a fake url parameter but I'd recommend against is, since <include> behaves differently when using url parameters - just to change the src (defer mode has all sorts of side effects you don't want to be worried about). So it's clearer to simply reset the src to null for an instant and force a re-render of the include like that.

Parameters passed into an <include> can be accessed with the @ExecutionArgParam annotation.

IMPORTANT: the order of parameters in the zul and when calling postNotifyChange matters. <include> needs to receive the data before changing the src-attribute (or it will re-render with the previous value) <include data="@load(navVM.data)" src="@load(navVM.page)"/>

BindUtils.postNotifyChange(null, null, this, "data");
BindUtils.postNotifyChange(null, null, this, "page");

I suggest sticking to a single "data" parameter which can be any java object and hence contain arbitrarily complex navigation information.

ZK-EE users should use the <apply>-shadow-element which doesn't require the forceReload approach e.g.:

<apply templateURI="@load(navVM.page)" data="@load(navVM.data)"/>

With <apply> the parameter order doesn't matter.

link publish delete flag offensive edit

Comments

Thanks, @cor3000, I'll look the link you've sent me. It seems a better choice putting in include this data property.

psinalberth ( 2018-03-14 04:41:17 +0800 )edit
0

answered 2018-03-09 23:41:20 +0800

psinalberth gravatar image psinalberth
52 6

I don't know if it's the better choice, but in my FormViewModel there is a @BindingParam("id") which binds the id selected in list.zul.

I did this on form.zul:

<div class="section" viewModel="@id('vm') @init('PersonFormViewModel', id = navigation.id)">
</div>

It works for static values, now I'll try with real objects.

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: 2018-03-09 21:46:59 +0800

Seen: 20 times

Last updated: Mar 13

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