2016-03-15 61 views
0

這是通過Lightswitch HTML客戶端的jscript 相同的例程可以很好地從Button調用,但調用失敗來自Canvas事件,顯示未定義的數據錯誤。相同的數據例程「CalcPrecio」可以正常從按鈕調用,但在從Canvas事件中調用時在Lightswitch HTML客戶端中斷開

// 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; 
    }); 
}; 

//我做錯了什麼? Heeeelp!

回答

0

此問題是從畫布'mousedown事件處理程序調用您的DibTriangulo和CalcPrecio例程的結果,只有屏幕作爲第一個參數。

在您的mousedown事件處理程序和_render closure中定義的屏幕對象並未專門設置爲指向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方法時使用它)。

相關問題