【问题标题】:zkoss MVVM changes to model force grid to reloadzkoss MVVM 更改为模型强制网格重新加载
【发布时间】:2012-12-19 17:27:34
【问题描述】:

我正在使用 ZKOSS MVVM。 所以在 View 中我使用了一个 Listbox,它被绑定(@load)到 ViewModel 中的一个列表模型对象。

如果我更改模型,我从文档中了解的内容

1:从索引 0 处的视图模型中添加一个对象到列表模型

I should see the latest object be appended at top of the Listbox.

2:从模型中删除一个项目

I should see that particular row from Listbox be removed.

注意:这是一个类似于社交网络的界面,例如当有人创建帖子并将新帖子附加到帖子列表时的 Facebook 墙。如果帖子被删除,则只会从列表中删除该帖子

嗯,确实会发生(新项目被追加/删除的项目被删除)但整个列表框会重新加载,而不仅仅是添加或删除的特定行。

这是为什么呢?为什么列表框会在列表模型更改时完全重新加载。

有什么想法吗?

这里是代码 sn-ps(用例:添加新帖子适用。在创建新帖子时,整个列表框每次都会重新加载):

查看

<z:div style="height: 100%; padding: 0px; margin: 0px;" apply="org.zkoss.bind.BindComposer"
    viewModel="@id('want_vm') @init('want.WantDesktopVM')">
<z:div zclass="content">
    <g:render template="../css/list/noEffectList"></g:render>
    <z:div hflex="1" width="100%" visible="@load(want_vm.toggleInput)" style="margin-bottom: 5px; padding: 5px">
        <z:vbox>
            <z:textbox id="postInput" multiline="true" value="" width="690px" height="50px"/>
            <z:div hflex="1" width="100%" style="text-align: right; padding-right: 5px">
                <z:button label="Post" zclass="button rect theme" onClick="@command('post', text=postInput.value)"/>
            </z:div>
        </z:vbox>           
    </z:div>
    <z:listbox model="@load(want_vm.posts)" emptyMessage="No new posts found." style="border:none;">
        <z:template name="model" var="iwant">
            <listitem style="margin-top: 10px"> 
                <listcell>
                    <hbox hflex="true">
                        <div zclass="dpFrame small">
                            <image height="50px" width="50px" content="@load(iwant.from) @converter('converter.UserActorDisplayPicConverter')" />
                        </div>
                        <vbox hflex="true" zclass="post"> 
                            <hbox hflex="true">
                                <label value="@load(iwant.from) @converter('converter.ActorDisplayNameConverter')" zclass="displayName"/>
                            </hbox>
                            <hbox hflex="true">
                                <label value="@load(iwant.textData)" zclass="post_data" multiline="true" maxlength="25"/>
                            </hbox>
                            <hbox>
                                <label value="@load(iwant.dateCreated) @converter('converter.SinceDateConverter')" zclass="since"/>
                            </hbox>
                        </vbox>
                    </hbox>
                </listcell> 
            </listitem>
        </z:template>
    </z:listbox>
</z:div>

视图模型

class WantDesktopVM {
UserActorManagerService userActorManagerService
ActivityManagerService activityManagerService

UserActor me
UserActor profile

String error = null
String view = 'iwant'

@Wire
Textbox postInput

private List<Activity> posts = []

@Init
public void init(@ContextParam(ContextType.COMPONENT) Component component,
@ContextParam(ContextType.VIEW) Component view) {
    profile = Executions.current.getAttribute("profile")
    me = Executions.current.getAttribute("me")
    loadPosts()
}

@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
    Selectors.wireComponents(view, this, false);
}

public boolean isMyProfile() {
    return me.id == profile.id
} 

public UserActor getMe() {
    return this.me
}

public boolean isToggleInput() {
    return this.view == 'iwant' && isMyProfile()
}

public List<Activity> getPosts() {
    println "Getting posts ...${posts.size()}"
    return this.posts
}

private List<Activity> loadPosts() {
    if(view == 'iwant') {
        posts = Activity.createCriteria().list() {
            eq 'from', profile
            eq 'type', ACTIVITY_TYPE.WANT
            order("lastUpdated", "desc")
        }
    } else {
        posts = ActorActivitySpace.createCriteria().list() {
            projections {property("activity")}
            eq 'actor', profile
            activity {
                ne 'from', profile
                eq 'type', ACTIVITY_TYPE.WANT
            }
            order("lastUpdated", "desc")
        }
    }
    return posts
}

@NotifyChange(['posts', 'toggleInput'])
@Command
public void render(@BindingParam('view') String view) {
    println "Changing view ..."
    this.view = view
    loadPosts()
}

@NotifyChange('posts')
@Command
public void post(@BindingParam('text') String text) {
    println "Posting text: $text"
    postInput.setValue("")
    if(text) {
        Activity want = activityManagerService.want(me.id, text)
        println"Want ID : $want.id"
        posts.addAll(0, [want])
    }
}

}

【问题讨论】:

  • 请始终提供代码。有不同的方法可以添加/删除导致不同行为的组件。
  • 我已经用代码 sn-ps 更新了帖子。请看一下
  • Activity.createCriteria().list()返回什么样的列表?在哪里将此列表连接到网格?
  • 它返回一个Hibernate POJO的列表(ArrayList):List

标签: data-binding mvvm grid zk


【解决方案1】:

您使用@NotifyChange('posts') 告诉 ZK 整个列表已更改。网格不会尝试检查列表,它只是将其当前的ListModel 替换为新列表 -> 完全重新加载。

如果你不想这样,你将不得不使用网格使用的ListModel 的方法来更新用户界面。这样,网格将准确地知道哪些行发生了变化,并且只更新那些。

[编辑]要实现您想要的,请将List&lt;Activity&gt; posts 替换为ListModelList&lt;Activity&gt; posts = new ListModelList&lt;Activity&gt;()

当活动发生变化时,您必须更新此列表(即调用add()addAll())以更新各个行。您不能再从数据库中加载所有内容,您必须将数据库中的更改合并与现有列表。

【讨论】:

  • 您可以参考我的任何特定文档吗?你说的对我来说很有道理,但我不知道他们的文档中哪里提到过?
  • @AlamSher:如何将视图模型连接到网格?上面的 ZUL 代码中没有 grid 元素。
  • 请立即查看。我已经用正确的场景和代码更新了这个问题。我尝试了太多东西,所以在发布时我忘记了我已经更改了代码。现在从 SVN 中检索它:)
  • @AaronDigulla 我同意你的观点,@NotifyChange('posts') 是问题所在,但你的解决方案让读者认为ListModel 接口提供了一个已磨损的add() 方法。您提到的那些方法是ListModelList&lt;&gt; 的一部分,我同意这是解决问题的最简单方法。
  • :) 那行得通。使用 ListModelList 的 add() 和 addAll() 当然避免 @NotifyChange('posts') 确实为我解决了这个问题。我希望这也适用于 remove() 。谢谢...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 2010-10-20
  • 2018-06-30
相关资源
最近更新 更多