Hi,

I found this thread http://tapestry-users.832.n2.nabble.com/Confirm-mixin-won-t-cancel-when-in-zone-td5048950.html#a5048950 but it's not exactly what I'm trying to do because it's for eventlinks. I'm a bit stuck getting the pieces of a custom mixin I'm trying to write to work, and my talent with writing mixins at this point is still a bit lacking.

I'm trying to make a mixin that attaches to a submit form element and is fired when the submit button is clicked. In my case, this submit button will always be a delete button. The mixin will trigger javascript to present a confirm box to the user, capture the result, and feed this back into a request parameter. A page class event then handles this request and sets a boolean that can escape deletion logic in the form submission event handler.

As of now, it seems like somewhere along the pipeline things are falling apart. The mixin code is adapted from ZoneUpdater, since I know that works and it pipelines in a manner that works for me. Without further ado is my related code for scrutiny. :

TML:

<t:submit class="t-beaneditor-submit" value="${message:delete-product}" t:id="Delete" t:mixins="deleteConfirm" t:deleteEvent="deleteConfirm" t:clientEvent="onclick" />

Mixin:

@IncludeJavaScriptLibrary("context:mixins/delete_confirm.js")
public class DeleteConfirm {
    // Parameters

    /**
* The event to listen for on the client. If not specified, zone update can only be triggered manually through
     * calling updateZone on the JS object.
     */
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String clientEvent;

    /**
     * The event to listen for in your component class
     */
@Parameter(name="deleteEvent", defaultPrefix = BindingConstants.LITERAL, required = true)
    private String event;

    @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "default")
    private String prefix;

    @Parameter
    private Object[] context;

    // Useful bits and pieces

    @Inject
    private ComponentResources resources;

    @Environmental
    private RenderSupport renderSupport;

    /**
     * The element we attach ourselves to
     */
    @InjectContainer
    private ClientElement element;

    // The code

    void afterRender() {
String url = resources.createEventLink(event, context).toAbsoluteURI();
        String elementId = element.getClientId();
        JSONObject spec = new JSONObject();
        spec.put("url", url);
        spec.put("elementId", elementId);
        spec.put("event", clientEvent);

// Tell Tapestry to add some javascript that instantiates a DeleteConfirm for the element we're mixing into. // Tapestry will put it at the end of the page in a section that runs once the DOM has been loaded. // The DeleteConfirm class it refers to is NOT THIS class - it is actually the one defined in delete_confirm.js. renderSupport.addScript("%sDeleteConfirm = new DeleteConfirm(%s)", prefix, spec.toString());
    }
}

Javascript:

// A class that displays a confirm box that returns a boolean value
// For a page class event handler to capture to conditionally
// Escape a delete action

function addRequestParameter(name, value, url) {
    if (url.indexOf('?') < 0) {
        url += '?'
    } else {
        url += '&';
    }
    value = escape(value);
    url += name + '=' + value;
    return url;
}

var DeleteConfirm = Class.create({

    initialize : function(spec) {
        this.element = $(spec.elementId);
        this.url = spec.url;

        if (spec.event) {
            this.event = spec.event;
this.element.observe(this.event, this.checkConfirm.bindAsEventListener(this));
        }
    },

    checkConfirm : function() {
        var zoneManager = Tapestry.findZoneManager(this.element);

        if (!zoneManager) {
            return;
        }

        var updatedUrl = this.url;

        if (this.element.value) {
            var param = this.element.value;

var okay_delete = confirm("Are you sure you want to delete " + param + "?");


updatedUrl = addRequestParameter('confirm',okay_delete, updatedUrl);

        }
        zoneManager.updateFromURL(updatedUrl);
    }

});


Page Class:

/* DELETE CONFIRMATION */

    @Inject
    private Request _request;
    @Persist
    private boolean okay_to_delete;

    void onDeleteConfirm(){
        String report = _request.getParameter("confirm");
        okay_to_delete = Boolean.parseBoolean(report);
debug("In onDeleteConfirm. Alright to delete? " + okay_to_delete); //this is not reached
    }


    void onSuccessFromForm(){
        /* snip */
            if(formMode.equals(FormMode.DELETE)){
                try {

                    if(!okay_to_delete)
                        return; //skip out

                    pdao.delete(prod);
                } catch (DAOException e) {
                    error("Failed to delete the product!");
                    e.printStackTrace();
                }
            }
        }
    }

    Thanks,
    Rich



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

Reply via email to