Alpaca - 是否有 onReady 表格?

Alpaca - Is there a form onReady?

使用 Alpaca js 我正在尝试即时使用另一种羊驼形式的 setValue 我得到

SyntaxError: JSON.parse: unexpected end of data at line 8 column 5 of the JSON data

我的猜测是它的发生是因为表单尚未呈现?有没有办法设置它的 "onReady" 功能?

演示场景的简单示例:

$(document).ready(function(){
 createForm("alpacaForm1","firstName","Copy from this form","FORM1 value","string");  
});


function updateForm(){
  createForm("alpacaForm2","firstName","To this form", "FORM2 value","string");      
   $("#alpacaForm2").alpaca().setValue($("#alpacaForm1").alpaca().getValue());

}
function createForm(divName,fieldName,fieldLable,fieldVal,fieldType){

$("#" + divName).alpaca({
    "data": { fieldName: fieldVal },
    "schema": {
        "type": "object",
        "properties": {
            fieldName: {
                "type": fieldType,
                "title": fieldLable
            }
        }
    }
});

}
<!-- jquery -->
        <script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
 
        <!-- bootstrap -->
        <link type="text/css" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
        <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
 
        <!-- handlebars -->
        <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js"></script>
 
        <!-- alpaca -->
        <link type="text/css" href="//cdn.jsdelivr.net/npm/alpaca@1.5.27/dist/alpaca/bootstrap/alpaca.min.css" rel="stylesheet" />
        <script type="text/javascript" src="//cdn.jsdelivr.net/npm/alpaca@1.5.27/dist/alpaca/bootstrap/alpaca.min.js"></script>
        
<script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script>

<div id="alpacaForm1" ></div>
<div id="alpacaForm2" ></div>
<button onclick="updateForm()">create form 2 and cop from form 1 </button>

postRender 允许在呈现控件时执行操作。

使用此回调的一种方法是修改 createForm 以接收 postRender 回调函数作为参数并将其配置为表单的选项。

$(document).ready(function() {
  const postRender = () => {};
  createForm("alpacaForm1", "firstName", "Copy from this form", "FORM1 value", "string", postRender);
});


function updateForm() {
  const postRender = control => control.setValue($("#alpacaForm1").alpaca().getValue());
  createForm("alpacaForm2", "firstName", "To this form", "FORM2 value", "string", postRender);
}

function createForm(divName, fieldName, fieldLable, fieldVal, fieldType, postRender) {

  $("#" + divName).alpaca({
    "data": {
      fieldName: fieldVal
    },
    "schema": {
      "type": "object",
      "properties": {
        fieldName: {
          "type": fieldType,
          "title": fieldLable
        }
      }
    },
    "postRender": postRender
  });

}
<!-- jquery -->
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>

<!-- bootstrap -->
<link type="text/css" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

<!-- handlebars -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js"></script>

<!-- alpaca -->
<link type="text/css" href="//cdn.jsdelivr.net/npm/alpaca@1.5.27/dist/alpaca/bootstrap/alpaca.min.css" rel="stylesheet" />
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/alpaca@1.5.27/dist/alpaca/bootstrap/alpaca.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script>

<div id="alpacaForm1"></div>
<div id="alpacaForm2"></div>
<button onclick="updateForm()">create form 2 and cop from form 1 </button>

减少 DOM 操作的更好方法是转发数据以覆盖第二个表单数据作为 createForm 函数中的参数。

function createForm(divName, fieldName, fieldLable, fieldVal, fieldType, dataOverrides={}) {

  $("#" + divName).alpaca({
    "data": {
      fieldName: fieldVal,
      ...dataOverrides
    },
    "schema": {
      "type": "object",
      "properties": {
        fieldName: {
          "type": fieldType,
          "title": fieldLable
        }
      }
    },
  });
}

然后在创建第二个表单时从第一个表单传递值。

function updateForm() {
  const dataOverrides = $("#alpacaForm1").alpaca().getValue();
  createForm("alpacaForm2", "firstName", "To this form", "FORM2 value", "string", dataOverrides);
}