Search code examples
javascriptjqueryvisual-studio-lightswitchlightswitch-2013

Lightswitch HTML custom control show only once


I've a LS project with one Custom Control where I render a chart with chartjs. The first time I load the page everything is ok, but if I move to another page and then come back the custom control is not rendered anymore (the code is called but nothing appear). I have to press F5 everytime I want to see it!

How can I fix this?

UPDATE: I've notice that if I set a global variable, when I come back to the screen the variable is not reinizialized .Also another page containing the jquery-week calendar has the same issue. When I navigate away from a screen and later come back I load another instance of the "screen" or only get the old one, with all the stuff already loaded?

here the code

/// <reference path="~/GeneratedArtifacts/viewModel.js" />

function updateGraphs(chart, query, inizio, fine) {
    var last = 0;
    for (var i = inizio; i <= fine; i.addDays(1)) {
        query(i).execute().then(function (res) {
            try {
                if (chart.datasets[0].points.length > 25)
                    chart.removeData();
                var date = new Date(inizio.valueOf()).addDays(last++);
                var str = format_2digit(date.getDate()) + "/" + format_2digit(date.getMonth() + 1) + "/" + date.getFullYear();
                chart.addData([res.results.length], last % 5 == 0 ? str : "");
            }
            catch (e) { alert(e); }
        });
    }
}

myapp.Home.created = function (screen) {
    var now = new Date();
    var month = now.getMonth();
    var year = now.getFullYear();

    screen.GraficoInizio = new Date(year, month, 1);
    screen.GraficoFine = new Date(year, month + 1, 0);
};

myapp.Home.GraficoFine_postRender = function (element, contentItem) {
    contentItem.dataBind("screen.GraficoInizio", function (value) {
        var month = contentItem.screen.GraficoInizio.getMonth();
        var year = contentItem.screen.GraficoInizio.getFullYear();
        contentItem.value = new Date(year, month + 1, 0);
    });
};

myapp.Home.GraficoClienti_render = function (element, contentItem) {
    // creo il grafico

    $(element).append("<canvas id='chartClienti' width='500px' height='300px' />");
    var ctx = $('#chartClienti').get(0).getContext("2d");
    var chart = new Chart(ctx);

    var data = {
        labels: [],
        datasets: [
            {
                label: [],
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(220,220,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: []
            }
        ]
    };

    chart = chart.Line(data, { animationSteps: 15 });
    contentItem.dataBind("screen.GraficoInizio", function (value) {
        updateGraphs(chart, myapp.activeDataWorkspace.ApplicationData.AbbonamentiAttivi,contentItem.screen.GraficoInizio, contentItem.screen.GraficoFine);
    });
    contentItem.dataBind("screen.GraficoFine", function (value) {
        updateGraphs(chart, myapp.activeDataWorkspace.ApplicationData.AbbonamentiAttivi, contentItem.screen.GraficoInizio, contentItem.screen.GraficoFine);
    });
};

myapp.Home.StampaQRCode_execute = function (screen) {
    var popup = window.open("../QRCode.aspx");
    popup.focus();
};

Solution

  • var globalVariableForchartClienti;
    
    myapp.Home.GraficoClienti_render = function (element, contentItem) {
    // creo il grafico
    
    var chartClientiElementId = 'chartClienti_' + Math.floor((Math.random() * 999999) + 1);    
    $(element).append("<canvas id='" + chartClientiElementId + "' width='500px' height='300px' />");
    
    //You can use "globalVariableForchartClienti" to access custom element in any other function
    globalVariableForchartClienti = chartClientiElementId;
    
    var ctx = $('#chartClienti').get(0).getContext("2d");
    var chart = new Chart(ctx);
    
    var data = {
        labels: [],
        datasets: [
            {
                label: [],
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(220,220,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: []
            }
        ]
    };
    
    chart = chart.Line(data, { animationSteps: 15 });
    contentItem.dataBind("screen.GraficoInizio", function (value) {
        updateGraphs(chart, myapp.activeDataWorkspace.ApplicationData.AbbonamentiAttivi,contentItem.screen.GraficoInizio, contentItem.screen.GraficoFine);
    });
    contentItem.dataBind("screen.GraficoFine", function (value) {
        updateGraphs(chart, myapp.activeDataWorkspace.ApplicationData.AbbonamentiAttivi, contentItem.screen.GraficoInizio, contentItem.screen.GraficoFine);
    });
    

    };