Wicket - 包裹收藏模型“转型”

发布于 2024-12-18 17:02:04 字数 704 浏览 0 评论 0 原文

我有一个域对象,它有一个原始值的集合,这些值代表另一个域对象(“Person”)的主键。

我有一个 Wicket 组件,它采用 IModel>,并允许您查看、删除人员并将其添加到列表中。

我想编写一个实现 IModel> 的包装器,但它由 PropertyModel> 支持。来自原始域对象。

仅查看很容易(为了简洁起见,Scala 语法):

class PersonModel(wrappedModel: IModel[List[Long]]) extends LoadableDetachableModel[List[Person]] {
   @SpringBean dao: PersonDao =_
   def load: List[Person] = {
       // Returns a collection of Persons for each id
       wrappedModel.getObject().map { id: Long =>
          dao.getPerson(id)
       }
   }
}

但是我该如何编写这个以允许在原始长整型列表中添加和删除呢?

或者模型不是进行此翻译的最佳位置?

谢谢!

I have a domain object which has a collection of primitive values, which represent the primary keys of another domain object ("Person").

I have a Wicket component that takes IModel<List<Person>>, and allows you to view, remove, and add Persons to the list.

I would like to write a wrapper which implements IModel<List<Person>>, but which is backed by a PropertyModel<List<Long>> from the original domain object.

View-only is easy (Scala syntax for brevity):

class PersonModel(wrappedModel: IModel[List[Long]]) extends LoadableDetachableModel[List[Person]] {
   @SpringBean dao: PersonDao =_
   def load: List[Person] = {
       // Returns a collection of Persons for each id
       wrappedModel.getObject().map { id: Long =>
          dao.getPerson(id)
       }
   }
}

But how might I write this to allow for adding and removing from the original List of Longs?

Or is a Model not the best place to do this translation?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-12-25 17:02:04

你可以这样做:

class PersonModel extends Model<List<Person>> {

    private transient List<Person> cache;
    private IModel<List<String>> idModel;

    public PersonModel( IModel<List<String>> idModel ) {
      this.idModel = idModel;
    }

    public List<Person> getObject() {
      if ( cache == null ) {
        cache = convertIdsToPersons( idModel.getObject() );
      return cache;
    }

    public void setObject( List<Person> ob ) {
      cache = null;
      idModel.setObject( convertPersonsToIds( ob ) );
    }

}

这不是很好的代码,但它显示了总体思路。您需要考虑的一件事是如何在请求之间序列化整个过程,您最好扩展 LoadableDetachableModel

另一件事是缓存:它的存在是为了避免每次在请求中调用 getObject() 时都必须转换列表。在实践中您可能需要也可能不需要它(取决于很多因素,包括转换的速度),但如果您使用它,则意味着如果其他东西正在修改底层集合,则更改可能不会被拾取通过这个模型。

You can do something like this:

class PersonModel extends Model<List<Person>> {

    private transient List<Person> cache;
    private IModel<List<String>> idModel;

    public PersonModel( IModel<List<String>> idModel ) {
      this.idModel = idModel;
    }

    public List<Person> getObject() {
      if ( cache == null ) {
        cache = convertIdsToPersons( idModel.getObject() );
      return cache;
    }

    public void setObject( List<Person> ob ) {
      cache = null;
      idModel.setObject( convertPersonsToIds( ob ) );
    }

}

This isn't very good code but it shows the general idea. One thing you need to consider is how this whole thing will be serialised between requests, you might be better off extending LoadableDetachableModel instead.

Another thing is the cache: it's there to avoid having to convert the list every time getObject() is called within a request. You may or may not need it in practice (depends on a lot of factors, including the speed of the conversion), but if you use it, it means that if something else is modifying the underlying collection, the changes may not be picked up by this model.

恍梦境° 2024-12-25 17:02:04

我不太确定我理解你的问题,而且我不理解 Scala 的语法。

但是,要从列表中删除实体,您可以提供一个链接,使用您的 dao 简单地删除它。您必须使用转发器来填充您的人员列表,以便每个转发器条目都有自己的模型,可以将其传递到删除链接。

看一下这个 Wicket 示例使用带有转发器的链接来选择联系人。您只需要对其进行调整即可删除您的人员,而不是选择它。

至于修改原始的 Long 列表,您可以使用 ListView.removeLink() 方法来获取从后备列表中删除条目的链接组件。

I'm not quite sure I understand your question and I don't understand the syntax of Scala.

But, to remove an entity from a list, you can provide a link that simply removes it using your dao. You must be using a repeater to populate your Person list so each repeater entry will have its own Model which can be passed to the deletion link.

Take a look at this Wicket example that uses a link with a repeater to select a contact. You just need to adapt it to delete your Person instead of selecting it.

As for modifying the original list of Longs, you can use the ListView.removeLink() method to get a link component that removes an entry from the backing list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文