欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Play vue.js with constant value in SailsJS

程序员文章站 2023-02-13 18:37:12
SailsJS supplies a utility module called parasails, which defines two elements, and  to allow user to create AJAX form easily ......

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.