问题
I am new to sencha touch. I have a requirement for maintaining a database (not local storage)for the sencha touch application, can any body help me.
回答1:
If your using php to connect to mysql this is a simple app I modified from the sencha site.
First your 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();
}
});
And then contact.php declared in the store
<?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) . ');';
}
?>
And then connecting to the database (db_connect) with php
<?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);
}
?>
This a sencha touch 1 app but can easily be modified to work with sencha touch 2. Hope that helps
来源:https://stackoverflow.com/questions/10007805/i-want-an-sample-sencha-touch-application-that-deals-with-database