这是 Lightswitch HTML 客户端上的 jscript从按钮调用的相同例程工作正常,但在从 Canvas 事件调用时失败,显示未定义的数据错误。
// Creates a Canvas and add an eventlistner Works fine
myapp.AddEditLito.Montajes_render = function (element, contentItem) {
var itemTemplate = $("<canvas id='myCanvas' width='600' height='150'></canvas>");
var canvas = myCanvas;
myCanvas.addEventListener('mousedown', function (event) {
var context = canvas.getContext('2d');
DibTriangulo(screen, 50); // Draws a triangle, everything fine until here
// here is the problem,
CalcPrecio(screen, 1); // breaks with "Undefined Data.. etc."
}, false);
};
// Called from a Button, same routine works perfect!!
myapp.AddEditLito.Button1_execute = function (screen) {
CalcPrecio(screen, 1); // Works fine
};
// This routine Works perfect called from a button, but fails when called from a Canvas event!
function CalcPrecio(screen, col) {
$.each(screen.Liquidas.data, function (i, p) {
p.valor = p.cantidad * p.unitario;
});
};
我做错了什么?嘻嘻!
此问题是从画布的鼠标向下事件处理程序调用 DibTriangulo 和 CalcPrecio 例程的结果,第一个参数仅为 screen。
在鼠标按下事件处理程序及其定义的_render闭包中,屏幕对象未专门设置为引用 LightSwitch 屏幕实例,而只是引用 window.screen 对象。
要解决此问题,您只需将代码更改为使用 contentItem.screen,如以下修订后的 _render 方法所示:
myapp.AddEditLito.Montajes_render = function (element, contentItem) {
var itemTemplate = $("<canvas id='myCanvas' width='600' height='150'></canvas>");
var canvas = myCanvas;
myCanvas.addEventListener('mousedown', function (event) {
var context = canvas.getContext('2d');
DibTriangulo(contentItem.screen, 50); // Changed to pass in contentItem.screen
CalcPrecio(contentItem.screen, 1); // Changed to pass in contentItem.screen
}, false);
};
在按钮的_execute方法中使用仅屏幕的原因是因为_execute方法接收 LightSwitch 屏幕对象作为其参数(然后在调用 CalcPrecio 方法时使用该参数)。