Closed syakunin closed 9 years ago
This is not Android-specific as far as I can tell. Beans must be 'boxed' specially or else Chunk has no way of knowing they're not POJOs (eg, note the use of set() for POJO v. setToBean() for beans). I don't know what you mean by "POJO bean" since my understanding is that these are two completely different structures -- a POJO just has public attributes with no getters/setters and bean has private attributes with public setters/getters.
One possible workaround for making lists of beans visible to the template is to make a list of Map objects with the beans pre-boxed like so:
import com.x5.util.ObjectDataMap;
...
public static List<Map> boxListOfBeans(List<MyBean> list) {
if (list == null) return null;
List<Map> boxedList = new ArrayList<Map>();
for (MyBean bean : list) {
boxedList.add(ObjectDataMap.wrapBean(bean));
}
return boxedList;
}
Then you can pass it to the chunk like so:
List<MyBean> list = getBeans();
Chunk c = theme.makeChunk();
c.set("beans", boxListOfBeans(list));
Please attach some code so I can get a more specific understanding of what it is you want to do.
I was using the following structure:
public class Order {
private String name;
private List<OrderItem> items;
public String getName() {...}
public void setName(String name) {...}
public List<OrderItem> getItems() {...}
public void setItems(List<OrderItem> items) {...}
}
public class OrderItem {
private String name;
public String getName() {...}
public void setName(String name) {...}
}
In this structure I have set up an order like this:
Order order = getOrder();
Chunk c = theme.makeChunk();
c.setToBean("order", order);
Template:
...
<ul>
{% loop in $order.items as $x %}
<li>{$x.name}</li>
{% endloop %}
</ul>
...
and cannot display order items properties in template. Chunk iterates over them smoothly, because I can see generated rows, but they are filled with {$x.name}. As soon as I made OrderItem implement DataCapsule, everything went ok.
Cool, I'm glad you found a workaround. I am working on a feature for 2.6 that attempts to treat objects as beans automatically as soon as it discovers that the object has no public attributes. The DataCapsule stuff is really clunky so it will be nice to be able to handle standard beans (even when referenced in a list) with no mods.
Here is a pre-release jar in case you want to try it out: http://www.x5software.com/chunk/releases/chunk-templates-2.6.jar
Cannot loop a list of POJO beans under Android. Madrobots library is used.