Editor 'Save and New' instance functionality

Hello,

I’m attempting to find a solution for an Editor screen button to save the current instance (if requirements met) and open a new blank instance in the same Editor. I found the following link: https://github.com/cuba-labs/demo-64-templates. However, this seems to be outdated. Is there any new instruction of how to accomplish this functionality?

Thanks in advance,
Adam

Hi Adam,

Something like this should solve the problem:

@UiController("demo_Customer.edit")
@UiDescriptor("customer-edit.xml")
@EditedEntityContainer("customerDc")
@LoadDataBeforeShow
public class CustomerEdit extends StandardEditor<Customer> {

    @Inject
    private DataContext dataContext;

    @Subscribe("saveAndNewBtn")
    public void onSaveAndNewBtnClick(Button.ClickEvent event) {
        commitChanges().then(() -> {
            dataContext.clear();
            Customer newEntity = dataContext.create(Customer.class);
            getEditedEntityContainer().setItem(newEntity);
        });
    }
}
1 Like

This is great. Thank you. The only remaining issue is any entity instance created with this method does not show up on the browse screen without refreshing the screen, however if one is saved using the regular windowCommitAndClose action, it will automatically show up. Is there an extra step or something available to accommodate this?

Thanks in advance,
Adam

From your browser controller add an afterCloseHandler or afterCommitHandler to the edit action. In this handler, reload your table’s container. So whenever the screen is closed/commited it’ll refresh the data automatically.

Hello Weston,

Thank you for the reply. I’ve tried this and it doesn’t seem to affect the Browse screens data loader.

    @Install(to = "newBusinessesTable.edit", subject = "afterCommitHandler")
    private void newBusinessesTableEditAfterCommitHandler(NewBusiness newBusiness) {
        newBusinessesDl.load();
    }

I did the same this for the Create button’s AfterCommitHandler. Can you see what I may be doing incorrectly?

Thanks,
Adam

Alright, I figured out the second part mostly by referencing the following:

https://github.com/alexbudarov/cubasaveandnew/commit/f53c560a2140e4f97ea27be457879b86c89bec5e

It basically builds a list of all entities saved and upon return to the browser, loads all of those saved entities into the data container.

Thanks again for the help, gentlemen!
Adam

sorry, is there a way to commit but the text remains the same not null ?

Hello,

Are you looking for a new entity with the same field values, or the same entity loaded without clearing? The difference is a new entity will create a new Id, so upon saving, you will have two records with different IDs but the same field values.

If you’re looking for a new entity with the same field values as the saved entity, something like this would work:

    @Inject
    private DataContext dataContext;
    @Inject
    private Notifications notifications;

    @Subscribe("saveAndNewBtn")
    public void onSaveAndNewBtnClick(Button.ClickEvent event) {
        commitChanges().then(() -> {
            notifications.create()
                    .withDescription("Entity saved")
                    .withType(Notifications.NotificationType.HUMANIZED)
                    .show();

            // Save existing values to use on next entity
            Test test = getEditedEntity();

            // Remove previous entity from data context
            dataContext.clear();

            // Create the new entity for the existing screen
            Test newEntity = dataContext.create(Test.class);

            // Copy values saved from previous entity
            newEntity.setName(test.getName());
            newEntity.setTitle(test.getTitle());
            newEntity.setFoo(test.getFoo());

            // Set the new entity in the edited entity container
            getEditedEntityContainer().setItem(newEntity);
        });
    }

Note that in this example, Test is just what I named this entity for this example.
Adam

2 Likes