1

这是我的整个脚本:

    document.addEventListener("deviceready", onDeviceReady, false); //add event for phonegap

    function resync(){

    var serviceURL = document.forms["form1"]["ServiceURL"].value;

    html5sql.process(
         [
             "DELETE FROM employees"
         ],
         function(){
          alert("loading xml");
             loadXML(serviceURL);
         },
         function(error, statement){
             alert("Error: " + error.message + " when processing " + statement);
         }        
     );
    }

    function onDeviceReady() {

    html5sql.openDatabase("emp", "HTML5 Web SQL Database", 6*300000*300000);
    html5sql.process("CREATE TABLE IF NOT EXISTS employees (empid , firstName, lastName, dob, title, department, officePhone, cellPhone, email, spousename, spousedob, marriageanniversary, picture, PRIMARY KEY (`empid`));")
    html5sql.process("CREATE TABLE IF NOT EXISTS config (ServiceURL);")

    $(document).ready(function()
    {
      renderEmployeeList();
    });


    function renderEmployeeList(){

      html5sql.process( 
        [
          {
            "sql": "SELECT * FROM config",
            "success": function(tx, results) {
              var len=results.rows.length;
              if (len==null || len==""){//check if config(serviceURL) is empty. If it is, go to settings.
                alert("XML Path is missing. Please enter a path in the settings.");
                window.location = '#settings';
              }else{
                  html5sql.process(//if config exists, start rendering employees.
                    [
                      {
                        "sql": "SELECT * FROM employees",
                        "success": function(tx, results) {
                          alert("called output select");
                          var len=results.rows.length;
                          var i;
                          for(i=0; i<len; i++) {
                            $("#output").append('<a data-ajax="false" href="employeedetails.html?id=' + results.rows.item(i).empid + '"><li class="ui-btn ui-btn-up-c ui-btn-icon-right ui-li-has-arrow ui-li" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c"><div id="outputdiv" class="ui-content" style="cursor: pointer;">' + results.rows.item(i).firstName + ' ' + results.rows.item(i).lastName + '</div></li></a>');
                          }
                        }
                      }
                    ]
                  );
              }
            }
          }
        ]
      );

    }
    }

    function loadXML(XMLurl){
      alert (XMLurl);
      $.ajax({
          type: "GET",
          url: XMLurl,
          dataType: "xml",
          success: parseXml
        });  
    }

    function parseXml(xml)
    {

      $(xml).find("employee").each(function()
        {
          alert("each called");
          var $this = $(this),
          $empid = $this.attr("empid"),
          $firstName = $this.attr("firstName"),
          $lastName = $this.attr("lastName"),
          $dob = $this.attr("dob"),
          $title = $this.attr("title"),
          $department = $this.attr("department"),
          $officePhone = $this.attr("officePhone"),
          $cellPhone = $this.attr("cellPhone"),
          $email = $this.attr("email"),
          $spousename = $this.attr("spousename"),
          $spousedob = $this.attr("spousedob"),
          $marriageanniversary = $this.attr("marriageanniversary"),
          $picture = $this.attr("picture");

          alert("FName " + $firstName);

          html5sql.process(
            [
              {
                "sql": "INSERT INTO employees (empid , firstName, lastName, dob, title, department, officePhone, cellPhone, email, spousename, spousedob, marriageanniversary, picture) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                "data": [$empid, $firstName, $lastName, $dob, $title, $department, $officePhone, $cellPhone, $email, $spousename, $spousedob, $marriageanniversary, $picture],
              }
           ]
          );
        }
      )
    }




    /* Save Configuration */
    function saveconfig(){

      var serviceURL = document.forms["form1"]["ServiceURL"].value;

      html5sql.process(
        [
          {
            "sql": "DROP TABLE config"
          }
        ]
      );

      html5sql.process(
        [
          {
            "sql": "CREATE TABLE IF NOT EXISTS config (ServiceURL)"
          }
        ]
      );

      html5sql.process(
        [
          {
            "sql": "INSERT INTO config (ServiceURL) VALUES (?)",
            "data": [serviceURL],
            "success": function(){
            alert("URL Saved.");
            loadXML(serviceURL);
            }
          }
        ]
      );

    }

我使用了来自 html5sql.com 的 html5sql.js

函数 resync() 没有给出错误,事实上,它使用正确的 ServiceURL 调用 loadXML。但是远程xml中的变化并没有体现在这里。几天以来,我一直坚持这一点。任何形式的帮助将不胜感激。谢谢。

4

0 回答 0