Dan, thank you for pointing that out. That makes more sense. So, it looks
like now that my List of Cheeses will not get cached between pages saving
space? And this is because the model doesn't do anything for detach but
only does load, so every time, it will get it from the DAO?

Here is the code for my index page. Supporting code files are attached.

public class Index extends CheesrPage {
public Index() {

        CheeseDAO dao = new CheeseDAOImpl();
        CheeseDetach myDetach = new CheeseDetach(dao);

        CheeseList myCheeseList = new CheeseList("cheeses", myDetach, 
getCart());
        add(myCheeseList);

[snip]
}

On Wed, Jun 15, 2011 at 06:35:25PM -0700, Dan Retzlaff wrote:
> Look carefully at the ListView's constructor arguments. It wants an
> IModel<List<Cheese>>, not an IModel<Cheese> which is what your current
> CheeseDetach provides. Depending on your goals, you can either (1) change
> CheeseDetach.load() to call getCheeses(), or (2) change the constructor to
> accept a list of cheeses and retain a list of cheese IDs, and query for
> those cheeses individually in CheeseDetach.load().
> 
> On Wed, Jun 15, 2011 at 5:38 PM, Brian Lavender <br...@brie.com> wrote:
> 
> > I am trying to create a ListView using a detachable model, but I just
> > can't seem to figure out how to construct my DetachableModel. Basically, I
> > would like to create a detachable model and then pass it to my constructor
> > for a CheeseList.  This is built upon the code for the  examples for
> > Wicket in Action [1] by Dashorst in Chapter 4.3.  My DAO can return the
> > cheese based upon id or it can also return the list of cheeses. What
> > code do I need to put in CheeseDetach for my detachable model?
> >
> >
> >         CheeseDAO myDAO = new CheeseDAOImpl();
> >
> >         // Can I make CheeseDetach construct it using the DAO as follows?
> >         CheeseDetach myDetach = new CheeseDetach(myDAO);
> >
> >          // ListView can take list or Model as constructor.
> >          // How does the model work for a ListView?
> >          CheeseList myCheeseList = new CheeseList("cheeses", myDetach,
> > getCart());
> >
> >          // Add ListView to page
> >
> >          add(myCheeseList);
> >
> >
> > 1. https://code.google.com/p/wicketinaction/downloads/list
> > --
> > Brian Lavender
> > http://www.brie.com/brian/
> >
> > "There are two ways of constructing a software design. One way is to
> > make it so simple that there are obviously no deficiencies. And the other
> > way is to make it so complicated that there are no obvious deficiencies."
> >
> > Professor C. A. R. Hoare
> > The 1980 Turing award lecture
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> > For additional commands, e-mail: users-h...@wicket.apache.org
> >

-- 
Brian Lavender
http://www.brie.com/brian/

"There are two ways of constructing a software design. One way is to
make it so simple that there are obviously no deficiencies. And the other
way is to make it so complicated that there are no obvious deficiencies."

Professor C. A. R. Hoare
The 1980 Turing award lecture
package com.brie.dtoo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheeseDAOImpl implements CheeseDAO {
	private static final Map<Long, Cheese> cheeses = new HashMap<Long, Cheese>();

    static {
        cheeses.put(1L, new Cheese());
        cheeses.put(2L, new Cheese());
        cheeses.put(3L, new Cheese());

        Cheese cheese = cheeses.get(1L);
        cheese.setId(1L);
        cheese.setName("Gouda");
        cheese.setDescription("Gouda is a yellowish Dutch cheese named after the city of Gouda. The cheese is made from cow's milk that is cultured and heated until the curd is separate from the whey. About ten percent of the mixture is curds which are pressed into circular moulds for several hours.");
        cheese.setPrice(2.95);

        cheese = cheeses.get(2L);
        cheese.setId(2L);
        cheese.setName("Edam");
        cheese.setDescription("Edam (Dutch Edammer) is a Dutch cheese that is traditionally sold as spheres with pale yellow interior and a coat of paraffin. Its Spanish name is queso de bola, literally 'ball cheese'. It is named after the town of Edam in the province of North Holland[1], where the cheese is coated for export and for tourist high season. Edam which has aged for at least 17 weeks is coated with black wax, rather than the usual red or yellow.");
        cheese.setPrice(1.25);

        cheese = cheeses.get(3L);
        cheese.setId(3L);
        cheese.setName("Old Amsterdam");
        cheese.setDescription("Old Amsterdam is a Dutch gourmet cheese that is ripened to perfection and regularly checked for flavor. It is a gourmet cheese of exceptionally high and consistent quality, with a buttery mature aged Gouda flavor that cuts with ease.");
        cheese.setPrice(3.10);
    }

    
	public Cheese getCheese(Long id) {
		return cheeses.get(id);
	}

	public List<Cheese> getCheeses() {
		List<Cheese> tmpList = new ArrayList<Cheese>(cheeses.values());
		return tmpList ;
	}

}
package com.brie.dtoo;

import java.util.List;

public interface CheeseDAO {
	public Cheese getCheese(Long id);
	public List<Cheese> getCheeses();
}
package com.brie.dtoo.callbacks;

import java.util.List;

import org.apache.wicket.model.LoadableDetachableModel;

import com.brie.dtoo.Cheese;
import com.brie.dtoo.CheeseDAO;

public class CheeseDetach extends LoadableDetachableModel<List<Cheese>> {

	private static final long serialVersionUID = 1L;
	private CheeseDAO myDAO;
	
	public CheeseDetach(CheeseDAO dao) {
		myDAO = dao;
	}

	@Override
	protected List<Cheese> load() {
		return myDAO.getCheeses();
	}

}
package com.brie.dtoo.callbacks;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
//import org.apache.wicket.model.IModel;

import com.brie.dtoo.Cart;
import com.brie.dtoo.Cheese;

public class CheeseList extends ListView<Cheese> {

	private static final long serialVersionUID = 1L;
	private Cart myCart;
	public CheeseList(String id, IModel<? extends List<? extends Cheese>> model, Cart cart) {
		super(id, model);
		myCart = cart;
	}

	@Override
	protected void populateItem(ListItem<Cheese> item) {
		Cheese cheese = item.getModelObject();
		item.add(new Label("name", cheese.getName()));
		item.add(new Label("description",
				cheese.getDescription()));
		item.add(new Label("price", "$" + cheese.getPrice()));
		item.add(new Link<Cheese>("add", item.getModel()) {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				Cheese selected =  getModelObject();
				myCart.getCheeses().add(selected);
			}
		}
		
		);
		
	}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to