0

ZK6: How to getr context in the ViewModel

asked 2011-12-09 07:49:33 +0800

Matze2 gravatar image Matze2
773 7

updated 2011-12-09 07:50:39 +0800

I am planning to port (at least to try) an existing application to ZK6.

What I am missing in all the sample code I've seen until now, is an example how to get typical context information like

* arg - from Executions.createComponents(...)
* param - from an HTTP request
* session - the HTTP session

into the ViewModel.

Of course there is Executions.getCurrent(), but there are also some subtle issues with that.
For example, it may contain wrong information if it is called in an event/command handler.
I guess it would be best to retrieve all that needed information in the constructor.

In the good ol' composer world, I've never had to take care about that. The instance variables arg, param, etc. always contained the correct values.
ViewModel is now a POJO, so different approaches are needed.

So are there some best practices how to achieve that?

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2011-12-09 10:30:13 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2011-12-21 05:04:17 +0800

I post a feature here, http://tracker.zkoss.org/browse/ZK-667
Currently, annotate @Init on a method of viewmodel with one parameter which type is BindContext

@Init
public void myinit(BindContext ctx){
..
}

it is the correct time to get value from Executions.getCurrent()

===
Supports after 12/21

@BindingParam (same as @Param, @Param was deprecated) to get binding arg
@ExecutionParam to get current execution attribute
@ExecutionArgParam to get current execution arg attribute
@ScopeParam to get attribute from scopes
@ContextParam to get special context object (ex, execution, desktop, session...)
@SelectorParam to get components by selector.

can use @*Param both in @Init and @Command, for example
@Init
public void myinit(@ExecutionArgParam("mode") String mode){
..
}

link publish delete flag offensive edit

answered 2011-12-09 12:22:40 +0800

Matze2 gravatar image Matze2
773 7

updated 2011-12-09 12:24:08 +0800

Thanks for tracking it and for the "workaround".

link publish delete flag offensive edit

answered 2011-12-15 12:11:15 +0800

avidD gravatar image avidD
166 2

Hi,
I am also currently struggling with passing arguments between composers. I used put them into the args using Exceutions.createComponents(String, Component, Map). As I understand, the execution pushes the arg map into a list which it must be popped from lateron. Sometime ZK takes care about popping it, sometimes not. Now what I need to do is
I have a form in which I can edit resources that are displayed in a tree. When the user clicks save / apply I want to update the view of the resource in the tree. Therefore, I must tell the view model which tree item(s) to update when the data was successfully updated. The form displaying the resource is opened by clicking on a tree item. In the listener I push the arguments into the execution:

Executions.getCurrent().pushArg(
        new ScreenModel.Args()
            .selectedItems(item.getTree().getSelectedItems())
            .getArgMap());

Then the nested form is composed, I call
mRootComponent = 
      Executions.createComponents(content.getSubscreen().getZul(), mWest, null);

where the zul file includes the nested form that displays the selected resource. The respective composer does not require any arguments, hence the parameter is null (and should be ignored as defined by the javadoc).
In the view model of the form however, the execution contains an empty map (before the required map). So this is the contents of the _args of the execution: [{}, {selectedItems=[<Treeitem w2KQ64>]}]. Now, am I doing something wrong? Maybe there is an easier / cleaner way to pass arguments?

Regards,
David

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2011-12-09 07:49:33 +0800

Seen: 942 times

Last updated: Dec 15 '11

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