如何使用 Tapestry 创建多个购物篮选择

发布于 2024-09-17 18:03:08 字数 433 浏览 2 评论 0 原文

让我们考虑以下问题。

有一个包含购物篮的页面。篮子是一个包含一系列物品的组件,例如水果或汽车等。

页面中共有三个篮子:汽车篮子、水果篮子和全篮子。车篮里装的是汽车,果篮里装的是水果,全篮可以同时装汽车和水果。

最初,只有汽车篮和水果篮里有物品。通过单击这些购物篮中的商品,该商品将被移动到所有购物篮中。通过单击所有购物篮中的商品,商品将移回到原来的购物篮中。

此外,水果物品和汽车物品的渲染方式也不同。例如,汽车项目可能包含与水果项目不同类型的背景。项目也可能包含不同类型的信息。例如,car-item 包含汽车的最大速度,fruit-item 包含水果的颜色。

此效果图也必须保留在全篮中。

您将如何使用 Tapestry 制作页面?我不需要完整的实施。我只是对解决这个问题的原则感兴趣。

另外,为了简化实施,您能否估计一下需要多少时间?

Let's consider following problem.

There is a page that contains baskets. A basket is a component that contains a list of items such as fruits or cars or whatever.

In the page there are three baskets: car-basket, fruit-basket and all-basket. The car-basket contains cars, fruit-basket contains fruits and all-basket can contain both cars and fruits.

Initially there are items only in the car- and fruit-baskets. By clicking item in those baskets the item will be moved to the all-basket. By clicking item in the all-basket, item will be moved back to it's original basket.

Also, fruit-items and car-items are rendered differently. For instance car-item may contain a different kind of background than fruit-item. Also item may contain different kind of information. For instance car-item contains car's maximum speed and fruit-item contains the color of the fruit.

This rendering must be retained also in the all-basket.

How would you do the page with Tapestry? I do not need a full implementation. I am merely interested in the principles how that problem could be solved.

Also, for simplified implementation, can you give some estimation of how much time it would take?

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

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

发布评论

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

评论(2

來不及說愛妳 2024-09-24 18:03:08

首先,您需要为汽车和水果创建组件。汽车和水果篮的显示就只是 循环项目,显示每个项目的水果/汽车组件。

混合篮子有点棘手。 Tapestry 不太擅长动态结构(“动态行为,静态结构”),因此您需要一个 if 构造来渲染汽车或水果组件。

<t:loop t:source="items" t:value="currentItem">
    <t:if t:test="currentCar">
       <span t:type="Car" t:car="currentCar" />
    </t:if>
     <t:if t:test="currentFruit">
       <span t:type="Fruit" t:fruit="currentFruit" />
    </t:if>
</t:loop>

currentItem 不是汽车时,该组件中的 getCurrentCar() 方法应返回 null,而当该项目是汽车时,getCurrentFruit() 应返回 null不是水果。

要使内容在列表之间移动,您可以使用 ActionLinks 以及适当的事件处理程序。要支持 AJAX,您可以使用 Tapestry 的内置 区域功能。要一次更新多个区域,您可以返回 来自事件处理程序的 MultiZoneUpdate 对象。 (或者您可以编写自己的优化客户端代码。)

您应该能够在很短的时间内建立这个基本结构,总共需要多长时间取决于您希望事情变得多么复杂。

For a start, you'd create components for cars and fruits. The display of the cars and fruits baskets then just loops through the items, displaying a fruit/car component for each of them.

The mixed basket is a little trickier. Tapestry is not very good at dynamic structures ("dynamic behaviour, static structure"), so you'd need an if construct to render either a car or a fruit component.

<t:loop t:source="items" t:value="currentItem">
    <t:if t:test="currentCar">
       <span t:type="Car" t:car="currentCar" />
    </t:if>
     <t:if t:test="currentFruit">
       <span t:type="Fruit" t:fruit="currentFruit" />
    </t:if>
</t:loop>

A getCurrentCar() method in that component should return null when currentItem is not a car, and getCurrentFruit() should return null when the item is not a fruit.

To make things move between lists, you can just use ActionLinks with appropriate event handlers. To support AJAX, you could use Tapestry's built-in Zone functionality. To update multiple zones at a time, you can return a MultiZoneUpdate object from your event handler. (Or you could write your own, optimized client-side code.)

You should be able to set up this basic structure in very little time, how long it'll take you in total depends on how elaborate you want things to get.

很酷又爱笑 2024-09-24 18:03:08

这是亨宁方法的延续:

<!-- render car basket -->
<t:loop t:source="carBasket" t:value="currentItem">
    <t:if t:test="currentCar">
       <span t:type="Car" t:car="currentCar" />
       <!-- TODO : eventlink for add to all basket -->
    </t:if>
</t:loop>

<!-- render fruit basket -->
<t:loop t:source="fruitBasket" t:value="currentItem">
     <t:if t:test="currentFruit">
       <span t:type="Fruit" t:fruit="currentFruit" />
       <!-- TODO : eventlink for add to all basket -->
    </t:if>
</t:loop>

<!-- render all basket -->
<t:loop t:source="allBasket" t:value="currentItem">
    <t:if t:test="currentCar">
       <span t:type="Car" t:car="currentCar" />
       <!-- TODO : eventlink for back to car basket -->
    </t:if>
     <t:if t:test="currentFruit">
       <span t:type="Fruit" t:fruit="currentFruit" />
       <!-- TODO : eventlink for back to fruit basket -->
    </t:if>
</t:loop>

在页面中你会看到这样的内容

@Property
private Item currentItem;

public Set<Item> getAllBasket(){
    return allBasket;
} 

public Set<Car> getCarBasket(){
    return carBasket;
} 

public Set<Fruit> getFruitBasket(){
    return fruitBasket;
}

public Fruit getCurrentFruit(){
   return currentItem instanceof Fruit ? (Fruit)item : null;
}

public Car getCurrentCar(){
    return curretItem instanceof Car ? (Car)item : null;
}

Here is a continuation of Henning's approach :

<!-- render car basket -->
<t:loop t:source="carBasket" t:value="currentItem">
    <t:if t:test="currentCar">
       <span t:type="Car" t:car="currentCar" />
       <!-- TODO : eventlink for add to all basket -->
    </t:if>
</t:loop>

<!-- render fruit basket -->
<t:loop t:source="fruitBasket" t:value="currentItem">
     <t:if t:test="currentFruit">
       <span t:type="Fruit" t:fruit="currentFruit" />
       <!-- TODO : eventlink for add to all basket -->
    </t:if>
</t:loop>

<!-- render all basket -->
<t:loop t:source="allBasket" t:value="currentItem">
    <t:if t:test="currentCar">
       <span t:type="Car" t:car="currentCar" />
       <!-- TODO : eventlink for back to car basket -->
    </t:if>
     <t:if t:test="currentFruit">
       <span t:type="Fruit" t:fruit="currentFruit" />
       <!-- TODO : eventlink for back to fruit basket -->
    </t:if>
</t:loop>

In the page you would have something like this

@Property
private Item currentItem;

public Set<Item> getAllBasket(){
    return allBasket;
} 

public Set<Car> getCarBasket(){
    return carBasket;
} 

public Set<Fruit> getFruitBasket(){
    return fruitBasket;
}

public Fruit getCurrentFruit(){
   return currentItem instanceof Fruit ? (Fruit)item : null;
}

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