Jquery移动页面显示以前查看过的数据

时间:2013-01-29 16:11:11

标签: cordova jquery-mobile

我正在构建一个JQM / Phonegap应用程序并且有一个恼人的问题。

我有一个页面显示来自websql / sqllite数据库的数据,只有2-3个字段(#view?id = 123)。第一次查看此页面时,一切正常(我抓取数据并将值添加到JQM pagebeforeshow事件中的字段)。

如果您再次查看该页面的其他记录(#view?id = 234),则会出现此问题。它显示包含最后一次查看数据的页面,然后在大约1-2秒后将其更新为当前记录的值。

有没有办法阻止此页面缓存(问题是 - 我没有使用任何JQM缓存数据属性),还是应该尝试清空pagehide事件中的字段?

提前致谢。

讨论的示例代码:

$('#view').live('pagebeforeshow', function(event, data) {

    // Clear any interval counter 
    clearInterval(window.viewTimerIntervalID);

    // On page load get passed url vars
    if ($.mobile.pageData && ($.mobile.pageData.caseRef || $.mobile.pageData.id)){
        if ($.mobile.pageData.caseRef) {
            $('input#edit-case-ref').val($.mobile.pageData.caseRef);
        }
        if ($.mobile.pageData.id) {
            $('input#edit-recordId').val($.mobile.pageData.id)
        }
    }

    // Grab record ID from hidden field
    var editId = $('input#edit-recordId').val();

    // form fields
    var caseTypeSelect = $('select#edit-case-type-select'),
        taskTypeSelect = $('select#edit-task-type-select'),
        caseRef = $('input#edit-case-ref'),
        notes = $('textarea#edit-notes'),
        editTimer = $('#edit-time h1'),
        startButton = $('#editStartStopRecording'),
        findCaseRef = $('a#findCaseRef');
    var startButtonTextSpan = $('#editStartStopRecording').prev('span').find('span.ui-btn-text');
    startButton.button('refresh');


    // Empty values of fields
    caseTypeSelect.val([]);
    taskTypeSelect.val([]);
    caseRef.val('');
    notes.val('');
    editTimer.text("0:00:00");


    // Get options for first select
    function getCaseTypes(tx) {
        tx.executeSql('SELECT * FROM CASETYPES', [],
            function(tx,results){
                var len = results.rows.length;

                // Remove any options already appended.
                $('select#edit-case-type-select option').remove();

                // Cache CaseType Select
                var caseTypeSelect = $('select#edit-case-type-select');

                // Append the first empty blank value option
                caseTypeSelect.append('<option value="" data-placeholder="true">Select Case Type</option');

                for (var i=0; i<len; i++){
                    // Append Select with details
                    caseTypeSelect.append('<option value="' + results.rows.item(i).id +'">' + results.rows.item(i).description + '</option>');
                }

                // Refresh the list to add JQM styles etc
                caseTypeSelect.trigger("change");
            },
            errorCB
        );
    }

    // Get options for second select
    function getTaskTypes(tx) {
        tx.executeSql('SELECT * FROM ACTIONTYPES', [],
            function(tx,results){
                var len = results.rows.length;

                // Remove any options already appended.
                $('select#edit-task-type-select option').remove();

                // Cache CaseType Select
                var taskTypeSelect = $('select#edit-task-type-select');

                // Append the first empty blank value option
                taskTypeSelect.append('<option value="" data-placeholder="true">Select Task Type</option');

                for (var i=0; i<len; i++){

                    // Append Select with details
                    taskTypeSelect.append('<option value="' + results.rows.item(i).id +'">' + results.rows.item(i).description + '</option>');
                }

                // Refresh the list to add JQM styles etc
                taskTypeSelect.trigger("change");
            },
            errorCB
        );
    }

    // populate the selects with values from DB
    function getRecording(tx) {
        tx.executeSql('SELECT * FROM RECORDINGS WHERE id=?', [editId],
            function(tx,results){
                var len = results.rows.length;

                for (var i=0; i<len; i++){

                    // Add values to selects
                    caseTypeSelect.val(results.rows.item(i).caseTypeId);
                    taskTypeSelect.val(results.rows.item(i).actionTypeId);

                    // Store this start date as a global var
                    window.editStartDate = results.rows.item(i).startDate;

                    // Add value to inputs
                    caseRef.val(results.rows.item(i).caseRef);

                    // Add notes
                    notes.val(results.rows.item(i).notes);

                    // Refresh the list to add JQM styles etc
                    caseTypeSelect.trigger("change");
                    taskTypeSelect.trigger("change");

                    // Calculate the time if still recording and start timer
                    if (!results.rows.item(i).endDate) {

                        // No end date so still recording....
                        // Need to work out diff between start and now
                        // And increment

                        var start = new Date(results.rows.item(i).startDate);
                        var end = new Date();
                        var dateDiff =  end.getTime() - start.getTime();

                        milliSecs = dateDiff

                        // 1000 milliseconds in a second
                        msSecs = (1000)
                        // Convert to seconds
                        msMins = (msSecs * 60)
                        // Convert to mins
                        msHours = (msMins * 60)
                        // Floor and calc to hours
                        numHours = Math.floor(milliSecs/msHours)
                        // Calc Mins
                        numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins)
                        // Calc secs 
                        numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs)
                        // Add leading zeros
                        if (numSecs < 10){
                          numSecs = "0" + numSecs
                        }
                        if (numMins < 10){
                          numMins = "0" + numMins
                        }

                        // Calc results
                        timeOnLoad = numHours + ":" + numMins + ":" + numSecs;

                        // Add to timer
                        editTimer.text(timeOnLoad);

                        // Start the incrementor and put the incrementor id in a global var (wuldnt work inside a local var for some reason).
                        window.viewTimerIntervalID = setInterval(editCount,1000);

                        // Change the button
                        startButtonTextSpan.text('Pause');
                        startButton.buttonMarkup({theme: 'g'}); //Red
                        startButton.buttonMarkup({icon: 'minus'}); // Swap out for custom icons

                        // Add the stop class
                        startButton.addClass('stop');

                        // Disable all fields as still recording
                        caseTypeSelect.selectmenu('disable');
                        taskTypeSelect.selectmenu('disable');
                        caseRef.textinput('disable');
                        notes.textinput('disable');
                        findCaseRef.addClass('ui-disabled');


                    // Calculate the time if enddate exists
                    } else {

                        // There is an end time - so calc diff and append
                        // Get start and end date, convert to JS date objects and subtract
                        var start = new Date(results.rows.item(i).startDate);
                        var end = new Date(results.rows.item(i).endDate);
                        var dateDiff =  end.getTime() - start.getTime();

                        milliSecs = dateDiff

                        // 1000 milliseconds in a second
                        msSecs = (1000)
                        // Convert to seconds
                        msMins = (msSecs * 60)
                        // Convert to mins
                        msHours = (msMins * 60)
                        // Floor and calc to hours
                        numHours = Math.floor(milliSecs/msHours)
                        // Calc Mins
                        numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins)
                        // Calc secs 
                        numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs)
                        // Add leading zeros
                        if (numSecs < 10){
                          numSecs = "0" + numSecs
                        }
                        if (numMins < 10){
                          numMins = "0" + numMins
                        }

                        // Calc results
                        resultString = numHours + ":" + numMins + ":" + numSecs;

                        // Append data
                        editTimer.text(resultString);

                        // Change the button
                        startButtonTextSpan.text('Resume');
                        startButton.buttonMarkup({theme: 'f'}); //Green
                        startButton.buttonMarkup({icon: 'plus'});

                        // Add the stop class
                        startButton.addClass('resume');

                        // Enable all fields as not recording
                        caseTypeSelect.selectmenu('enable');
                        taskTypeSelect.selectmenu('enable');
                        caseRef.textinput('enable');
                        notes.textinput('enable');
                        findCaseRef.addClass('ui-enabled');

                    }
                }
            },
            errorCB
        );
    }

    // Populate select options from DB
    db.transaction(getCaseTypes, errorCB);
    db.transaction(getTaskTypes, errorCB);

    // Populate the fields with record details
    db.transaction(getRecording, errorCB);
});

也不确定它是否相关,但我使用的是多页模板(所有#pages在一个html文档中)。再次感谢。

2 个答案:

答案 0 :(得分:1)

您可以尝试禁用缓存:

$ .mobile.page.prototype.options.domCache = false;

您可以尝试从localStorage中删除特定元素:

localStorage.removeItem(elementHere);

希望它有所帮助。

答案 1 :(得分:0)

您可以清除pagebeforeshow事件处理程序中的值。

编辑:

您需要编排SQL事务和JQM页面更改。

1)从SQL Tx处理程序

中启动pageChange

OR

2)清除pagebeforeshow中的现有值,然后启动SQL Tx。在Tx成功回调中可用时在屏幕上填充值。您可以在此等待期间显示loading动画。