Editor event submitSuccess

Editor event submitSuccess

rf1234rf1234 Posts: 2,990Questions: 87Answers: 421
edited September 2017 in General

It would be great if the Editor event submitSuccess also returned the action that was executed successfully. If you need to know the action you can't use submitSuccess right now.

My workaround is to use postSubmit and check whether the action was completed sucessfully like this. I found out that I definitely have to check for json.error and json.fieldErrors because if they exist you don't get all the other parameters that you may need to check for.

.on('postSubmit', function (e, json, data, action) {
    if (! json.error && ! json.fieldErrors) {
        if (action === 'remove') {
            do something
        }
        if (action === 'edit') {
          do something else
        }
        if ( action === 'create' &&
             typeof json.data[0] !== 'undefined'  ) {
            do something else
        }
    }
});

Maybe this extension can be put into the next relase?!

Replies

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    You could use the postCreate, postEdit and postRemove events separately.

    Alternatively, you could call a function that take the action value as a parameter and returns your common handler function as a closure, that way you can use a single function to handle postCreate/Edit/Remove if you wish and still have access to the action.

    For example:

    function handler(action) {
        return function(e, json, data) {
            // this function is returned from handler, and becomes the function bound to the event, but it's a closure so still has access to the outer function (handler) scope. 
            
            // do stuff
            // can use outer 'action' parameter inside this function
    
            if (action === 'remove') {
                do something
            }
            if (action === 'edit') {
              do something else
            }
            if ( action === 'create' &&
                 typeof json.data[0] !== 'undefined'  ) {
                do something else
            }
        }
    }
    
    editor.on('postCreate', handler("create"));
    editor.on('postEdit', handler("edit"));
    editor.on('postRemove', handler("remove"));
    

    But I agree, in some cases it would be quicker to have the action value sent to the submitSuccess event.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Added to the enhancement list - it will be in 1.7.0.

    Thanks for the suggestion!

    Allan

  • rf1234rf1234 Posts: 2,990Questions: 87Answers: 421

    Thanks Allan!
    @rduncecb: Thanks for your comment! Looks like a good solution, too!

    Once Allan will have implemented the change I guess postCreate, postEdit and postRemove are more or less redundant. I never used those events because you have code duplication in some cases (e.g. same logic for Create and Edit).

This discussion has been closed.