我想要一个处理数据库的样本sencha touch应用程序

时间:2012-04-04 08:50:08

标签: extjs sencha-touch

我是sencha touch的新手。我需要为sencha touch应用程序维护数据库(不是本地存储),任何人都可以帮助我。

1 个答案:

答案 0 :(得分:3)

如果您使用php连接到mysql这是一个我从sencha网站修改的简单应用程序。

首先是你的javascript

var helloWorld = new Ext.Application({

    launch: function() {

        Ext.regModel('Contacts', {
            fields:[{
                name:'first_last_name'
            }]
        });

        store = new Ext.data.Store({
            model: 'Contacts',
            proxy: {
                type: 'scripttag',
                url: 'contact.php',
                reader: {
                    type: 'json',
                    root: 'emergency_contact'
                },
                extraParams:{
                    action:'read'
                }
            }
        });



        var tmpl = new Ext.Template(
            '<table>',
            '<tr>',
            '<td>',

            '{first_last_name}',

            '</td>',
            '</tr>',
            '</table>'
            );

        this.tabs = new Ext.TabPanel({
            fullscreen: true,
            dockedItems: [{
                xtype:'toolbar', 
                title:'Hello World'
            }],
            tabBar: {
                ui: 'light',
                layout: {
                    pack: 'center'
                }
            },
            items: [
            {
                html:'Hello',
                title:'Hello'
            },

            {
                html:'world', 
                title:'world'
            },

            {
                cls: 'list',
                title: 'list',
                xtype: 'list',
                store: store,
                itemTpl:tmpl
            }
            ]
        });
        this.list = this.tabs.items.getAt(2);


        store.load();
    }

});

然后在商店中声明contact.php

<?php
header('Content-Type: text/javascript; charset=UTF-8');

ini_set("display_errors", true);
ini_set("html_errors", true);

include "db_connect.php";

$out = "";
$action = "";
$data = "";
$contact = "";

if (isset($_REQUEST["action"])) {
    $action = $_REQUEST["action"];
}
switch ($action) {
    case "read": $out = read_this();
        break;
}
echo utf8_encode($out);

function read_this() {

    $conn = OpenDbConnection();

    $sql = "select first_last_name from contact";



     $result = mysql_query($sql);

    $num = mysql_numrows($result);

    $i = 0;

    $eData = array("count" => $num, "contact" => array());

    while ($row = mysql_fetch_assoc($result)) {
        $eData["contact"][$i] = $row;
        $i++;
    }

    CloseDbConnection($conn);
  //  return json_encode($eData);
    return $_REQUEST['callback'] . '(' . json_encode($eData) . ');';
}

?>

然后使用php

连接到数据库(db_connect)
<?php
function OpenDbConnection() {


  $dbhost = 'localhost:3306';
  $dbuser = 'phpapp';
  $dbpass = 'phpapp';
  $dbname = 'dbname';



  $conn = mysql_connect($dbhost, $dbuser, $dbpass);

  if (!$conn) {
    echo( "Unable to connect to the database server." );
    exit();
  }

  mysql_select_db($dbname) or die( "Error selecting database.");

  return $conn;
}

function CloseDbConnection($conn) {
  mysql_close($conn);
}
?>

这是一个sencha touch 1应用程序但可以轻松修改以使用sencha touch 2.希望有所帮助