我是煎茶触摸的新手。我需要为 sencha touch 应用程序维护一个数据库(不是本地存储),任何人都可以帮助我。
问问题
5129 次
1 回答
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();
}
});
然后在 store 中声明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 一起使用。希望对您有所帮助
于 2012-04-04T17:55:32.637 回答