MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

SailsJS:Ottieni Resonse di Ajax-Form di SailsJS nella funzione inviata

Hai ragione nei passaggi che hai descritto sopra e penso che tutto ciò che ti manca è che devi inserire un parametro nella tua funzione inviata. Come supporto nel modello vue, passi ($event). Nello script della pagina (nome-pagina.pagina.js) puoi fare in modo che il parametro venga chiamato come vuoi dove definisci la funzione inviata.

Anche se non sembra che tu ne abbia bisogno, farò qui un esempio completo nel caso in cui qualcun altro abbia problemi con le funzioni ajax-form in Sails.js.

Nel tuo modello (html):

<ajax-form
    action="<camelcase of the file for your action>" 
    :handle-parsing="parseForm"
    :submitted="submittedForm($event)"
    @rejected="rejectedForm($event)"
    :form-data="formData"
    :form-rules="formRules"
    :form-errors.sync="formErrors"
    :cloud-error.sync="cloudError"
>
<input type="text" id="input1" v-model="input1">

Qui, form-data si riferirà a un oggetto in cui i dati vengono archiviati. Le chiavi proverranno da ciò che hai impostato il v-model' as for a given input. form-rulesis where you specify an object of objects. They key of each is the input name from v-modeland the value can be a string or array of strings for the rules set. form-errorsspecifies an object where errors will go if the form triggers any errors upon submission. If this happens, data does not get sent to the server and neither the submitted or rejected function get run. cloud-error.sync` specifica un oggetto in cui andranno gli eventuali errori di back-end se l'azione restituisce una risposta diversa da 200.

Nello script della tua pagina (nome-pagina.page.js):

data: {
    formData: {},
    formErrors: {},
    formRules: {
        input1: 'required'
    },
    cloudError: ''
},
methods: {
    parseForm: function () {
        // You can do parsing and custom validations here, but return all data 
        // you want to send to the server as an object called 'argins'
        return argins;
    },
    submittedForm (data) {
        // Here you can use any data that is returned from the action, like
        console.log('returned data: ', data);
    },
    rejectedForm (err) {
        // This function runs if the server returns a non-200 response
        console.log(err);
    }
}