我正在玩Dymo的JS框架。 original example无问题,但即使我不想点击,它也会加载XML。我只是在我调用时才重写执行Dymo代码:
<script type="text/javascript">
function printnow() {
$.get("./cont.label", function(labelXml) {
label = dymo.label.framework.openLabelXml(labelXml);
}, "text");
// open label
// set label text
label.setObjectText("MENO", "test");
label.setObjectText("MUNKA", "test");
label.setObjectText("DESCRIPTION", test);
// select printer to print on
// for simplicity sake just use the first LabelWriter printer
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0) throw "No DYMO printers are installed. Install DYMO printers.";
var printerName = "";
for (var i = 0; i < printers.length; ++i) {
var printer = printers[i];
if (printer.printerType == "LabelWriterPrinter") {
printerName = printer.name;
break;
}
}
if (printerName == "") throw "No LabelWriter printers found. Install LabelWriter printer";
// finally print the label
label.print(printerName);
};
</script>
不幸的是我打电话给printnow();我得到ReferenceError: label is not defined
问题在哪里?
答案 0 :(得分:1)
首先,您必须阅读此one。
简而言之,您正在处理异步代码。因此,此label.setObjectText("MENO", "test");
和其余代码将在此label = dymo.label.framework.openLabelXml(labelXml);
行之前执行。
最后,你必须做这样的事情:
<script type="text/javascript">
function printnow() {
$.get("./cont.label", function(labelXml) {
var label = dymo.label.framework.openLabelXml(labelXml);
// open label
// set label text
label.setObjectText("MENO", "test");
label.setObjectText("MUNKA", "test");
label.setObjectText("DESCRIPTION", test);
// select printer to print on
// for simplicity sake just use the first LabelWriter printer
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0) throw "No DYMO printers are installed. Install DYMO printers.";
var printerName = "";
for (var i = 0; i < printers.length; ++i) {
var printer = printers[i];
if (printer.printerType == "LabelWriterPrinter") {
printerName = printer.name;
break;
}
}
if (printerName == "") throw "No LabelWriter printers found. Install LabelWriter printer";
// finally print the label
label.print(printerName);
}, "text");
};
</script>