2013-02-04 38 views
3

我的任務是爲我們的中型學校董事會創建一個移動應用程序來宣傳我們的學校。最初,這看起來很簡單 - 我抓住了Cordova和Eclipse,扔了jQuery Mobile庫,併爲我的第一所學校設置了幾頁(我認爲總共有5個)。複製/粘貼/編輯內容/更新鏈接爲其他17.JQuery Mobile和Cordova頁面模板

然後我的老闆說「讓每個學校的顏色的頁面」。再次,容易 - peasy。之後有一些CSS類,所有的一百頁左右的頁面都很漂亮,並與學校相匹配。

然後來到了炸彈。到目前爲止,所有的數據都是本地的 - .htm文件,它們將與應用程序一起安裝,並在用戶點擊它們時由應用程序加載:無需數據連接。今天早上,有人問我是否可以在沒有應用程序更新的情況下即時更改信息。我傻傻地但如實地說,「不」 - 你可以猜測下一個請求是什麼。

所以,我需要一種方法來實現這一點。我現在的思考過程是運行一些服務器端接收某種類型的id號(appdata.schoolbord.web/123),它會返回一個包含數據的JSON對象 - 可能是標題,名稱頁面模板(這樣我就可以將我的93頁減少到只有幾頁),一些CSS(只是爲了阻止下一個請求),然後將數據滑入模板。

有沒有一種簡單的方法來使用Cordova和jQuery Mobile來做到這一點?任何方向的指針都會很棒。謝謝。

+0

你使用的是什麼樣的服務器端技術? – Gajotres

+0

服務器是Redhat上的標準LAMP堆棧。 –

回答

1

下面是一個簡單的例子。我創建了一個jQM登錄頁面,您需要輸入用戶名和密碼。這些數據將用於檢查數據庫中是否存在用戶名,如果存在則返回true,否則返回false。 我會建議你創建一個更好的數據庫讀取邏輯,這是一個簡單的解決方案,傾向於SQL注入,但它將適用於你的任務。

的index.php:

<!DOCTYPE html> 
<html> 
<head> 
    <title>jQM Complex Demo</title> 
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
    <style> 
     #login-button { 
      margin-top: 30px; 
     }   
    </style> 
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>  
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
    <script src="js/custom.js"></script> 
</head> 
<body> 
    <div data-role="page" id="login"> 
     <div data-theme="a" data-role="header"> 
      <h3>Login Page</h3> 
     </div> 

     <div data-role="content"> 
      <label for="username">Enter your username:</label> 
      <input type="text" value="" name="username" id="username"/> 
      <label for="password">Enter your password:</label> 
      <input type="password" value="" name="password" id="password"/> 
      <a data-role="button" id="login-button" data-theme="b">Login</a> 
     </div> 

     <div data-theme="a" data-role="footer" data-position="fixed"> 

     </div> 
    </div> 
    <div data-role="page" id="index"> 
     <div data-theme="a" data-role="header"> 
      <h3></h3> 
     </div> 

     <div data-role="content"> 
      Here goes content 
     </div> 

     <div data-theme="a" data-role="footer" data-position="fixed"> 
      <h3>Page footer</h3> 
     </div> 
    </div>  
</body> 
</html> 

json.php:

<?php 
    $var1 = $_REQUEST['action']; // We dont need action for this tutorial, but in a complex code you need a way to determine ajax action nature 
    $jsonObject = json_decode($_REQUEST['outputJSON']); // Decode JSON object into readable PHP object 

    $username = $jsonObject->{'username'}; // Get username from object 
    $password = $jsonObject->{'password'}; // Get password from object 

    mysql_connect("localhost","root",""); // Conect to mysql, first parameter is location, second is mysql username and a third one is a mysql password 
    @mysql_select_db("test") or die("Unable to select database"); // Connect to database called test 

    $query = "SELECT * FROM users WHERE user_name = '".$username."' and user_pass = '".$password."'"; 
    $result=mysql_query($query); 
    $num = mysql_numrows($result); 

    if($num != 0) { 
     echo "true"; 
    } else { 
     echo "false";   
    } 
?> 

custom.js:

$(document).on('pagebeforeshow', '#login', function(){ 
    $('#login-button').on('click', function(){ 
     if($('#username').val().length > 0 && $('#password').val().length > 0){ 
      userObject.username = $('#username').val(); // Put username into the object 
      userObject.password = $('#password').val(); // Put password into the object 
      // Convert an userObject to a JSON string representation 
      var outputJSON = JSON.stringify(userObject); 
      // Send data to server through ajax call 
      // action is functionality we want to call and outputJSON is our data 
      ajax.sendRequest({action : 'login', outputJSON : outputJSON}); 
     } else { 
      alert('Please fill all nececery fields'); 
     } 
    });  
}); 

$(document).on('pagebeforeshow', '#index', function(){ 
    if(userObject.username.length == 0){ // If username is not set (lets say after force page refresh) get us back to the login page 
     $.mobile.changePage("#login", { transition: "slide"}); // In case result is true change page to Index 
    } 
    $(this).find('[data-role="header"] h3').append('Wellcome ' + userObject.username); // Change header with wellcome msg 
    //$("#index").trigger('pagecreate'); 
}); 

// This will be an ajax function set 
var ajax = { 
    sendRequest:function(save_data){ 
     $.ajax({url: 'http://localhost/JSONP_Tutorial/json.php', 
      data: save_data, 
      async: true, 
      beforeSend: function() { 
       // This callback function will trigger before data is sent 
       $.mobile.showPageLoadingMsg(true); // This will show ajax spinner 
      }, 
      complete: function() { 
       // This callback function will trigger on data sent/received complete 
       $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner 
      }, 
      success: function (result) { 
       if(result == "true") { 
        $.mobile.changePage("#index", { transition: "slide"}); // In case result is true change page to Index 
       } else { 
        alert('Login unsuccessful, please try again!'); // In case result is false throw an error 
       } 
       // This callback function will trigger on successful action 
      }, 
      error: function (request,error) { 
       // This callback function will trigger on unsuccessful action     
       alert('Network error has occurred please try again!'); 
      } 
     }); 
    } 
} 

// We will use this object to store username and password before we serialize it and send to server. This part can be done in numerous ways but I like this approach because it is simple 
var userObject = { 
    username : "", 
    password : "" 
} 

如果你想,觸點M e在我的電子郵件上(您可以在ma profile中找到它),我將向您發送源代碼項目並使用sql腳本。

+0

這不是我真正想要的 - 我可以聯繫到服務器並獲取數據,沒有任何問題......我在嘗試在客戶端進行模板化時遇到了更多問題。 –