Mounting Dynamic Data with JavaScript

0

I'm working on a hybrid application, I'm using HTML5 with Java Script, my idea is when the buddy logs in with his account, the application goes on the server and searches everything that is related to that person and already mounts on a listview all the rooms of that comrade.

function PesquisaTurma(){
EscreverConsole(console, "Pesquisando turmas");
var read_transition = db.transaction("tbl_TURMAS", "readonly"),
    store = read_transition.objectStore("tbl_TURMAS"),
    rows = store.openCursor(),  
    flag,
    HTMLNovo,
    index,
    $wrapper;
rows.onsuccess = function(evt){
    var cursor = evt.target.result;
    if(cursor)
    { 
    var bd =    db.transaction("tbl_PESSOA_TURMA").objectStore("tbl_PESSOA_TURMA");

        index = bd.index("COD_IDENT_TURMA");
        index.get(cursor.value.COD_IDENT_TURMA).onsuccess = function(event) { 
            $wrapper = document.querySelector('#turma'),

                // Pega a string do conteúdo atual
                var HTMLTemporario = $wrapper.innerHTML;

            flag = event.target.result.FLG_IDENT_PESSO;

            // Novo HTML que será inserido
            HTMLNovo = '<ion-item class="item widget btn_ESCOLHE_TURMA item-avatar" data-uib="ionic/list_item_avatar" data-ver="0">'+'<input id="w_codigo" type="hidden" value="'+cursor.value.COD_IDENT_TURMA+'"/>'+'<img src="images/icon36.png">'+'<h2>'+cursor.value.TXT_NOMEX_TURMA+'</h2>'+ '<p>'+((flag == "M") ? "Você é aluno." : "Você é professor")+'</p>'+'</ion-item>'; 

            HTMLNovo = HTMLNovo + HTMLTemporario;

            // Coloca a nova string(que é o HTML) no DOM
            $wrapper.innerHTML = HTMLNovo;

            //HTMLNovo = HTMLNovo + HTMLTemporario;

            // Coloca a nova string(que é o HTML) no DOM
           $('#turmas').append($wrapper);

        };
        cursor.continue();
    }
}
EscreverConsole(console, "Turmas pesquisadas com sucesso.");    
};

This code works cool, but 2 errors happen with it, but it is not programming.

  

1st Problem - When I log in, I call this function, but it does not place the classes immediately in the application, it is necessary to update the page for it to appear.   2nd Problem - If I log in twice, it doubles, and this time it does not need to refresh the page.

    
asked by anonymous 15.09.2015 / 13:28

1 answer

0

I've just added a for so it loads up everything. The error occurred because the classes were in memory, and when recharging it remained only one, making a for, it loads all the classes at a time.

Follow the code:

                for(i=0; i < cursor.length; i++) 
            { 
                // Coloca a nova string(que é o HTML) no DOM
                $('#turmas').append($wrapper);
            }
    
15.09.2015 / 19:59