I am in the middle of writing a form using Apache Wicket. The same page/class is used to add a new item to the database AND to edit an existing record in the database. Of course, since the page constructor is called only once, the model is always set to whatever record is initially loaded on the page, or a new record if we're not editing an existing one.
I have found a number of ways to dynamically load data, but they seem verbose and a little clunky. I suspect there is a best practice for handling a scenario like this.
For reference, here is some edited code:
public class JobManagement extends WebPage {
private static final long serialVersionUID = 1L;
private long jobId = 0;
protected void setJobId(long id) {
this.jobId = id;
}
protected long getJobId() {
return jobId;
}
public JobManagement() {
LoadableDetachableModel<Job> jobModel = new LoadableDetachableModel<Job>() {
private static final long serialVersionUID = 1L;
@Override
protected Job load() {
Job job = (Job) EntityFactory.getInstance().getBean("job");
// if we're editing an existing job, load the object
if (jobId >= 1) {
job.load(jobId);
}
return job;
}
};
add(new FeedbackPanel("feedbackPanel"));
Form<Job> jobForm = new Form<Job>("jobForm") {
private static final long serialVersionUID = 1L;
@Override
public void onSubmit() {
// Handles the form submit...
}
};
add(jobForm);
jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
// SNIP ... All my form fields go here!
jobForm.add(new Button("submit"));
}
}
I'm using a LoadableDetachableModel, but it's not entirely clear to me how to best handle loading it dynamically whenever the page is rendered. I've attempted to load a new instance of a Model, override the getObject() class which returns my LoadableDetachableModel, but there's something that feels very wrong about that. Any input would be appreciated. I've been trying to feel my way through this framework via online documentation exclusively, so forgive my evident lack of familiarity.