Play vue.js with constant value in SailsJS
2018-10-06 08:06:28来源:博客园 阅读 ()
SailsJS supplies a utility module called parasails, which defines two elements, <ajax-form> and <ajax-button> to allow user to create AJAX form easily.
In the most cases, the user could define a form as this in SailsJS
HTML
<ajax-form action="submitSomething" :cloud-error.sync="cloudError" :handle-parsing="handleParsingForm" @submitted="submittedForm()"> <input type="text" v-model="someInput" > <ajax-button type="submit" :syncing="syncing" class="btn btn-dark">Submit Something</ajax-button> </ajax-form>
JS
parasails.registerPage('test-page', { // ╦╔╗╔╦╔╦╗╦╔═╗╦ ╔═╗╔╦╗╔═╗╔╦╗╔═╗ // ║║║║║ ║ ║╠═╣║ ╚═╗ ║ ╠═╣ ║ ║╣ // ╩╝╚╝╩ ╩ ╩╩ ╩╩═╝ ╚═╝ ╩ ╩ ╩ ╩ ╚═╝ data: { // Main syncing/loading state for this page. syncing: false, // Form data formData: { /* … */ }, // For tracking client-side validation errors in our form. // > Has property set to `true` for each invalid property in `formData`. formErrors: { /* … */ }, // Server error state for the form cloudError: '', }, // ╦ ╦╔═╗╔═╗╔═╗╦ ╦╔═╗╦ ╔═╗ // ║ ║╠╣ ║╣ ║ ╚╦╝║ ║ ║╣ // ╩═╝╩╚ ╚═╝╚═╝ ╩ ╚═╝╩═╝╚═╝ beforeMount: function() { // Attach raw data exposed by the server. _.extend(this, SAILS_LOCALS); }, mounted: async function() { //… }, // ╦╔╗╔╔╦╗╔═╗╦═╗╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗ // ║║║║ ║ ║╣ ╠╦╝╠═╣║ ║ ║║ ║║║║╚═╗ // ╩╝╚╝ ╩ ╚═╝╩╚═╩ ╩╚═╝ ╩ ╩╚═╝╝╚╝╚═╝ methods: { handleParsingForm: function() { var argins = this.formData; return argins; }, submittedForm: async function() { // Executed after submission. }, } });
Once user clicks the button, the two callback functions would be executed, the function handleParsingForm takes the data from the form, and submittedForm is the callback after success.
How about we want to do a dynamic form which sends the dynamically generated constant data from the server at page, and send it to the form again ?
Change the HTML to this.
<button type="submit" class="btn" v-on:click="setValue(someValue)">Submit</button>
Here we use default button and set the callback to event v-on:click, and once button was clicked, it runs the function setValue to call the JavaScript function. What we have to do in the JavaScript is add new function.
methods: { setValue: function (someValue) { this.formData.someValue = someValue; }, }
So easy to pass the data into the form.
Thanks.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:koa源码解读
- vue.js开发环境搭建教程 2020-03-16
- JavaScript的with 语句的使用方法 2019-11-05
- vue.js(5)--事件修饰符 2019-08-14
- vue.js(4)--字符串跑马灯 2019-08-14
- vue.js(3)--v-bind与v-on 2019-08-14
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash