简体   繁体   中英

SAILS - How to close modal with ajax-form after receiving “notFound” error from action

So, I am using Sailsjs - and I am using a modal form to confirm deletion of an item. What I want to do is to detect a double deletion (someone deleted an item - while it was in someone else's page - and the second person tries to delete it too). I managed to do that - my action is returning "notFound". The problem is - how can I make the Modal close when getting "notFound" back from the action instead of success?

Here is an excerpt of my action code:

  exits: { forbidden: { description: 'The user making this request is not the owner of this thing and therefore cannot delete it.', responseType: 'forbidden' }, notFound: { description: 'The item was not found in the database. Maybe it has already been deleted?', responseType: 'notFound' } }, fn: async function (inputs, exits) { var thing = await Thing.findOne({ id: inputs.id }); if (!thing) { throw 'notFound'; } if (thing) { if (thing.owner !== this.req.me.id ) { throw 'forbidden'; } } await Thing.destroy({ id: inputs.id }); return exits.success(); } 

and here is an excerpt of my modal using ajax-form component:

  <% /* Confirm DeleteThing Modal */%> <modal v-if="confirmDeleteThingModalOpen" @close="closeDeleteThingModal()"> <ajax-form action="destroyOneThing" :syncing.sync="syncing" :cloud-error.sync="cloudError" :handle-parsing.sync="handleParsingDeleteThingForm" @submitted="submittedDeleteThingForm()"> <div class="modal-header"> <h3 class="modal-title">Are you sure?</h3> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> 

You can use @rejected on your ajax-form. So somethinglike:

<ajax-form action="destroyOneThing" @rejected="rejectedDeleteOneThing" 

Then tell it what you want it to do on your page script

something like:

    rejectedDeleteOneThing: function() {
      this.confirmDeleteThingModalVisible = false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM