zoukankan      html  css  js  c++  java
  • Play vue.js with constant value in SailsJS

    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.

  • 相关阅读:
    今天 弄了一下ajax 里面的
    重修理解了一下js 控制treeview 菜单的子级和父级的关系 理解的还不够全 因为 html不够全 但是加个注释吧
    xml .net 用法
    这几天在做把数据库里的数据 导出 并且压缩 学到了一些东西
    今天实现了用按钮后台动态实现了 table里面内容的显示和隐藏 在实现过程中了解了updatepanel的简单的用法
    今天第一次参加了软件产品的讨论会议 收获
    收藏个debug的文章
    Animate.css
    flash遨游缓存问题
    URL短地址压缩算法 微博短地址原理解析 (Java实现)
  • 原文地址:https://www.cnblogs.com/Jedimaster/p/9745508.html
Copyright © 2011-2022 走看看