Play vue.js with constant value in SailsJS
sailsjs supplies a utility module called , 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 , 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.
上一篇: 西汉时期有一个著名学者叫刘歆 这个刘歆为什么改名叫做刘秀
下一篇: koa源码解读