SailsJS:在提交的函数中获得SailsJS的Ajax形式的共鸣 [英] SailsJS: Get Resonse of Ajax-Form of SailsJS in the submitted function

查看:127
本文介绍了SailsJS:在提交的函数中获得SailsJS的Ajax形式的共鸣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个动作中获取一个对象,这个动作由ajax表单组件调用.

I want to get an object from an action, which is called by an ajax form component.

复制步骤.

  1. 创建一个ajax表单,您可以在其中传递模型的值(例如,帖子的标题和说明)

  1. create a ajax form where you passing values for a model (eg. title and description for a post)

在处理提交表单后,将数据传递给操作

After handling the submitting form let pass the data to a action

在操作中,您将给定的数据安全到MongoDB并使用.fetch()提取创建的数据

In the action you will safe the given data to the MongoDB and fetch that created data with .fetch()

您将获取的数据传递给exits.success(fetchedData)

You pass the fetched data to exits.success(fetchedData)

尝试在xxxx.page.js中的SubmittedForm函数中获取数据

Try to get the data in the submittedForm function in the xxxx.page.js

我无法获取数据.我记录了ajax-form.components.js.在第398行中,我们发出结果(结果应该具有我们的数据,就我而言,这是事实),但是在那之后,结果消失了. 也许我理解错了,显然我做错了.

I am not able to get the data. I logged the ajax-form.components.js. In line 398, we emit the result (the result should have our data, in my case it is the fact) but after that, the result is gone. Maybe I understand it wrong, obviously I do things wrong.

如果您需要更多信息,请告诉我.

If you need more info, let me know.

推荐答案

在上述步骤中,您是正确的,我想您所缺少的就是必须将Give参数添加到提交的函数中.作为vue模板中的道具,您传入($ event).在页面脚本(page-name.page.js)中,您可以在定义提交函数的位置随意命名该参数.

You are correct in the steps you have described above, and I think all that you're missing is that you have to put give parameter to your submitted function. As a prop in the vue template, you pass in ($event). In the page script(page-name.page.js) you can have the parameter be named whatever you want where you define the submitted function.

尽管看起来好像不需要它,但在这里我将举一个完整的示例,以防其他人在Sails.js中遇到Ajax形式的函数时遇到的麻烦.

Though it doesn't seem like you need it, I'm going to put a thorough example here in case anyone else is having trouble with ajax-form functions in Sails.js.

在您的模板(html)中:

In your template(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">

在这里,form-data将引用要存储数据的对象.密钥将来自您设置的内容v-model' as for a given input. form-rules is where you specify an object of objects. They key of each is the input name from v-model and the value can be a string or array of strings for the rules set. form-errors specifies 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`指定一个对象,如果操作返回,后端错误将消失非200响应.

Here, form-data will refer to an object that the data gets stored. The keys will come from what you set the v-model' as for a given input.form-rulesis where you specify an object of objects. They key of each is the input name fromv-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` specifies an object where any back end errors will go if the action returns a non-200 response.

在页面脚本(page-name.page.js)中:

In your page script (page-name.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);
    }
}

这篇关于SailsJS:在提交的函数中获得SailsJS的Ajax形式的共鸣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆