Revision history [back]

click to hide/show revision 1
initial version

asked 2013-09-18 18:05:00 +0800

rickcr gravatar image rickcr

How to reuse same zul and backing ViewModel - more than one instance in scope

I'm trying to not repeat myself (DRY.) I have a paginated listbox that I'd like to reuse as an include on two (or more) different zuls, but both zuls could be in memory at the same time.

I like to back my components, including my listbox with a ViewModel (I've explained elsewhere the benefits of this.)

The issue I'm having though is in communication when the listbox viewModel needs to send out an even such as "row selected." Obviously I only want the "selectedRow" picked up by the OUTER view model. The page that has this included listbox zul on it. However, since it's a global event and the listbox and its viewmodel could be in scope on other pages... this event could end up picked up in the wrong VMs.

What's the best practice to avoid this?

So for example, let's say you have a "Manufacturer" screen a "Customer" screen... both backed by a ViewModel. Both screens use an "Automobiles list box" with a backing AutomobilesViewModel"

When the "selectedAutomobile" is targeted, you only want the appropriate VM (Manufacturer or Customer VM) to be notified... but how do you accomplish this? Do you need to pass around some kind of id from the AutomobilesVM when the global command is sent out and then ignore it in a VM if it doesn't match some id you care about?

How to reuse same zul and backing ViewModel - more than one instance in scope

I'm trying to not repeat myself (DRY.) I have a paginated listbox that I'd like to reuse as an include on two (or more) different zuls, but both zuls could be in memory at the same time.

I like to back my components, including my listbox with a ViewModel (I've explained elsewhere the benefits of this.)

The issue I'm having though is in communication when the listbox viewModel needs to send out an even such as "row selected." Obviously I only want the "selectedRow" picked up by the OUTER view model. The page that has this included listbox zul on it. However, since it's a global event and the listbox and its viewmodel could be in scope on other pages... this event could end up picked up in the wrong VMs.

What's the best practice to avoid this?

So for example, let's say you have a "Manufacturer" screen a "Customer" screen... both backed by a ViewModel. Both screens use an "Automobiles list box" with a backing AutomobilesViewModel"

When the "selectedAutomobile" is targeted, you only want the appropriate VM (Manufacturer or Customer VM) to be notified... but how do you accomplish this? Do you need to pass around some kind of id from the AutomobilesVM when the global command is sent out and then ignore it in a VM if it doesn't match some id you care about?

[EDITED]

Not to get too long winded, but let me further illustrate a problem. You have a listbox that you want to reuse. However the "selectedRow" you only want firing on the row on the outer ViewModel backing the page that the include of the listbox is used on. I can't figure out tho make this happen... eg

The include file has..

 <listbox id="itemsList" model="@load(vm.tierItems)"
         selectedItem="@bind(vm.selectedTierItem)" ..>

In the above the setter for selectedTierTiem needs to reside on the ViewModel backing the listbox include. I'd like it to "trickle up" instead to the enclosing ViewModel. Since I can't seem to do this, I have to have this UGLY kind of "if the include is on X page logic" send this global command... eg

//in my listbox view model

 public void setSelectedTierItem(TierItem selectedTierItem) {
    this.selectedTierItem = selectedTierItem;

           if (pageType.equals("TIERS")) {
                    Map<String, Object> args = new HashMap<String,Object>();
        args.put("selectedTierItem", selectedTierItem);
        BindUtils.postGlobalCommand(null, null, "tierItemSelectedForFooBarViewModel", args);
    } else {
                  //if other page type the include is used on
           }
}

Do you see the issue I'm running into? It's not that easy to reuse a listbox on different views. I have to pass to 1) pass to the listbox view model what outer page I'm used on and 2) I have to different switches used within this Listbox view model to decide who to notify

It would be much nicer if somehow ZK would look for the outer VM somehow to handle the selected item, and there might be a way, I just don't know the syntax?

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