0

template model vs forEach

asked 2012-03-15 08:47:33 +0800

khcyt gravatar image khcyt
216 1
Hello, the document "Small Talks/2011/July/Envisage ZK 6.0: Rendering List and Tree Model with Templates" explains the new feature zk6 feature template/model. Can anyone explain to me the advantage, if I use this feature. Compared with the traditional method forEach/each. Source from Small Talk Example. The same functionality with the forEach method is given below. I think template/model feature is not necessary. It is worse to read. It needs more code
<zk>
    <zscript><![CDATA[
    ListModel quarters = new ListModelArray(new String[] {"Q1", "Q2", "Q3", "Q4"});
    Map months = new HashMap();
    months.put("Q1", new ListModelArray(new String[] {"Jan", "Feb", "Mar"}));
    months.put("Q2", new ListModelArray(new String[] {"Apr", "May", "Jun"}));
    months.put("Q3", new ListModelArray(new String[] {"Jul", "Aug", "Sep"}));
    months.put("Q4", new ListModelArray(new String[] {"Oct", "Nov", "Dec"}));
    ListModel qs = (quarters);
    ]]></zscript>
    <listbox model="${quarters}">
        <template name="model">
            <listitem>
                <listcell>${each}</listcell>
                <listcell>
                    <listbox model="${months}">
                        <template name="model">
                            <listitem label="${each}"/>
                        </template>
                    </listbox>
                </listcell>
            </listitem>
        </template>
    </listbox>
</zk>
<zk>
    <zscript><![CDATA[
    java.util.Map quarters = new java.util.TreeMap();
    quarters.put("Q1", new java.util.ArrayList(java.util.Arrays.asList(new String[] {"Jan", "Feb", "Mar"})));
     quarters.put("Q2", new java.util.ArrayList(java.util.Arrays.asList(new String[] {"Apr", "May", "Jun"})));
     quarters.put("Q3", new java.util.ArrayList(java.util.Arrays.asList(new String[] {"Jul", "Aug", "Sep"})));
     quarters.put("Q4", new java.util.ArrayList(java.util.Arrays.asList(new String[] {"Oct", "Nov", "Dec"})));
    ]]></zscript>
    <listbox>
            <listitem forEach="${quarters}">
                <listcell>${each.key}</listcell>
                <listcell>
                    <listbox>
                            <listitem forEach="${each.value}" label="${each}"/>
                    </listbox>
                </listcell>
            </listitem>
    </listbox>
</zk>
Kai
delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2012-03-16 12:37:52 +0800

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

If the data is readonly (render once when loading) then using forEach is OK.
However it cannot handle dynamic data but model can.

link publish delete flag offensive edit

answered 2012-03-19 08:37:25 +0800

khcyt gravatar image khcyt
216 1

Hello Dennis,

thanks for the explanation. So it makes perfect sense and I will use it in my projects. :-)

Kai

link publish delete flag offensive edit

answered 2012-05-22 15:24:41 +0800

kwalczak gravatar image kwalczak
9

updated 2012-05-22 15:29:36 +0800

Hi guys,
I've additional question regarding template stuff - is it possible to mixed dynamic content with static, please see example below

<zk>
    <zscript><![CDATA[
    ListModel quarters = new ListModelArray(new String[] {"Q1", "Q2", "Q3", "Q4"});
    Map months = new HashMap();
    months.put("Q1", new ListModelArray(new String[] {"Jan", "Feb", "Mar"}));
    months.put("Q2", new ListModelArray(new String[] {"Apr", "May", "Jun"}));
    months.put("Q3", new ListModelArray(new String[] {"Jul", "Aug", "Sep"}));
    months.put("Q4", new ListModelArray(new String[] {"Oct", "Nov", "Dec"}));
    ListModel qs = (quarters);
    ]]></zscript>
    <listbox model="${quarters}">
        <template name="model">
            <listitem>
                    <listcell>STATIC RENDERS ONCE </listcell>
            </listitem>
            <listitem>
                <listcell>${each}</listcell>
                <listcell>
                    <listbox model="${months}">
                        <template name="model">
                            <listitem label="${each}"></listitem>
                        </template>
                    </listbox>
                </listcell>
            </listitem>
        </template>
    </listbox>
</zk>


Right now I have to add this statict content to my model and I'm wondering if this cannot be done as I mentioned above ... any ideas ?

link publish delete flag offensive edit

answered 2012-05-23 02:08:25 +0800

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

apply to EL rule.
<listbox model="${months}"> (at here, each is Q1/Q2..., model is a ListModelArray in months.)
and
<listitem label="${each}"></listitem> (at here, each is Jan/Feb...)

link publish delete flag offensive edit

answered 2012-05-23 09:33:55 +0800

kwalczak gravatar image kwalczak
9

Hi Dennis,
Thanks for your reply but actually I don't get it ... anyway my above example is not consistent ...please find below updated version ...

<zk>
    <div apply="org.zkoss.bind.BindComposer" viewModel="@id('model') @init('foo.MyViewModel')">

    <listbox children="@{model.months}">
        <template name="children">
            <listitem>
                    <listcell>STATIC RENDERS ONCE </listcell>
            </listitem>
            <listitem>
                <listcell>${each}</listcell>
            </listitem>
        </template>
    </listbox>

   </div>
</zk>

..so I'm interested in output like that:
...
STATIC RENDERS ONCE
January
February
March
April
...
hopefuly this is better example ....

link publish delete flag offensive edit

answered 2012-06-11 10:17:16 +0800

zes gravatar image zes
30

Hi kwalczak,

if you find the answer, it interests me !
Thx

link publish delete flag offensive edit

answered 2012-06-13 09:52:36 +0800

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

kwalczak
I saw you binding syntax is not correctly,
Basically, you should use <listbox children="@load(model.months"/>
and in listcell , use <listcell label="@load(each)"/>
for the first static item, use @template and give different template
read this for example of dynamic template
http://books.zkoss.org/wiki/ZK_Developer's_Reference/MVVM/Data_Binding/Children_Binding
read this for iteration status
http://books.zkoss.org/wiki/ZK%20Developer's%20Reference/MVVM/Data%20Binding/Property%20Binding#Collection_Binding

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: 2012-03-15 08:47:33 +0800

Seen: 888 times

Last updated: Jun 13 '12

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