0

How to bind a Model with a Viewmodel

asked 2014-07-02 09:56:46 +0800

valnay gravatar image valnay
13 2

Hi everyone,

I'm currently writing a program using ZKoss MVVM pattern.

What I want to do is displaying live data received from a distant server. To do so I need to have it transferred through all the layers of my app automatically, so that when the server pushes data my end user's View gets updated.

Architecture :

View <--> ViewModel <--> Model <--> Server

  • I know how to update my Model with live data received from the server
  • View would get updated automatically through ViewModels's BindComposer

Where it gets tricky :

ViewModel <-- Model

I don't know the best pratice to get my ViewModel's properties updated once my Model has been updated.

I've though about implmenting the Observable/Observer pattern. The Model would be observed by the ViewModel and it would then get its properties updated automatically. I wanted to know if there's a better way to bind Models to ViewModels.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-07-25 17:03:48 +0800

Darksu gravatar image Darksu
1991 1 4

Hello valnay

A good example of how to accomplish your task is the following:

a. zul grid

<grid id="sampleGird" model="@load(vm.sampleModel)" 
<auxhead>
    <auxheader label="Sample Grid" colspan="1">     
    </auxheader>
</auxhead>
<columns>
    <column label="Users" width="100%"/>                                        
</columns>
<rows>
    <template name="model" var="item">
        <row>
            <cell style="text-align: left;">
                <label value="@load(user.name)"/>
            </cell>              
        </row>
    </template>
</rows>
</grid>

b. java class

In the java class you have to declare the following:

@Wire("#sampleGird")
private Grid sampleGird;

private ListModelList<User> sampleModel;

And you will use a method to update the model and the grid as shown bellow:

@GlobalCommand
@NotifyChange({"sampleModel", "sampleGird"})
public void populateGrid() {
    sampleModel = new ListModelList<>();

    User user = new User();
    user.setName("Sample User");

    List<User> userList = new ArrayList<User>();
    userList.add(user);

    sampleModel.addAll(userList);
}

If you need more help, i can provide you with a full working example.

Best Regards,

Darksu

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
1 follower

RSS

Stats

Asked: 2014-07-02 09:56:46 +0800

Seen: 33 times

Last updated: Jul 25 '14

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