0

我正在构建一个 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);
});

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

4

2 回答 2

1

您可以尝试禁用缓存:

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

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

localStorage.removeItem(elementHere);

希望能帮助到你。

于 2013-01-29T17:56:59.420 回答
0

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

编辑:

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

1)pageChange从 SQL Tx 处理程序中启动

或者

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

于 2013-01-29T16:36:20.700 回答