当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Play vue.js with constant value in SailsJS

Play vue.js with constant value in SailsJS

2018年10月07日  | 移动技术网IT编程  | 我要评论

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.

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网