情况:我使用HTML文档中的脚本(a(来使用特定的SDK。然后我使用第二个脚本(b(basic来创建一个Kendo UI表。
我的问题:我试图通过全局变量将数据从脚本(a(传递到脚本(b(,但它不起作用,我该怎么办?
可能对您有所帮助的信息:
- 我的文档是一个由标签框起来的表单
- 我用卡蒙达。第一个脚本允许我使用SDK来检索与正在处理的表单相关联的实例的ID。(但我不认为这是问题的关键(
- 我假设浏览器同时读取两个脚本,这就是为什么脚本(b(不能读取变量的原因,因为它还没有在脚本(a(中创建
代码:
<script type="text/javascript" src="http://localhost:8080/camunda/app/tasklist/scripts/kendo.all.min.js"></script> <script cam-script type="text/form-script"> var taskService = camForm.client.resource('task'); var processInstanceId = null; var result = null; taskService.get(camForm.taskId, function(err, task) { //alert(JSON.stringify(task)); debugger; processInstanceId = task.processInstanceId; $.get("http://localhost:8080/engine-rest/process-instance/"+processInstanceId+"/variables", function(result) { debugger; window.alert("coucou"); console.log(result.JsonWeightSetpoints.value); }); debugger; console.log(result.JsonWeightSetpoints.value); debugger; }); </script> <script type="text/javascript"> console.log(result.JsonWeightSetpoints.value); //this is where I implement the Kendo UI grid </script> <div id="grid"></div>
我无法读取脚本(b(中结果变量的内容,因为它没有定义。
我该怎么做?
自定义事件解决方案:
<script type="text/javascript" src="http://localhost:8080/camunda/app/tasklist/scripts/kendo.all.min.js"></script>
<script cam-script type="text/javascript">
var taskService = camForm.client.resource('task');
var processInstanceId = null;
var result = null;
taskService.get(camForm.taskId, function(err, task) {
//alert(JSON.stringify(task));
debugger;
processInstanceId = task.processInstanceId;
$.get("http://localhost:8080/engine-rest/process-instance/"+processInstanceId+"/variables", function(result) {
debugger;
window.alert("coucou");
console.log(result.JsonWeightSetpoints.value);
// the value is available here, we now can trigger an event and send it
document.dispatchEvent(new CustomEvent("handler", {
detail: { value: result.JsonWeightSetpoints.value }
}));
});
debugger;
console.log(result.JsonWeightSetpoints.value);
debugger;
});
</script>
<script type="text/javascript">
console.log(result.JsonWeightSetpoints.value);
//this is where I implement the Kendo UI grid
// event listener, this would get executed only when we want
document.addEventListener("handler", function(event) {
// write you logic here
console.log(event.detail.value);
});
</script>
<div id="grid"></div>
有用资源:
- MDN:创建和触发事件
- 引入异步JavaScript