准備
開始開發前,請先到下面的地址下載Sencha Touch 2的包:http://www.sencha.com/products/touch/download/ 。下載完解壓后你會發現包里有很多文件。里面有api文檔、開發包和一些實例等等。現在,我們只需要sencha-touch-debug.js和resources/css/sencha-touch.css文件即可。(sencha-touch-debug.js文件里面是未經壓縮的帶注釋的js代碼,方便我們閱讀和debug)。
包文件到手了,選上一個你喜歡的IDE,建立一個web項目並把文件引進吧。我選了Aptana Studio建立了以下目錄結構:

開始種碼
在根目錄新建一個app.html文件,在app目錄下新建一個app.js文件(用於編寫我們的js代碼)。然后,把需要的內容引進app.html文件中,如下:
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>First App</title>
- <link rel="stylesheet" href="js/lib/sencha-touch.css" type="text/css">
- <link rel="stylesheet" href="css/style.css" type="text/css">
- <script type="text/javascript" src="js/lib/sencha-touch-debug.js"></script>
- <script type="text/javascript" src="app/app.js"></script>
- </head>
- <body></body>
- </html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>First App</title>
<link rel="stylesheet" href="js/lib/sencha-touch.css" type="text/css">
<link rel="stylesheet" href="css/style.css" type="text/css">
<script type="text/javascript" src="js/lib/sencha-touch-debug.js"></script>
<script type="text/javascript" src="app/app.js"></script>
</head>
<body></body>
</html>
1.打開 app/app.js文件,正式開始編寫我們第一個Sencha Touch App了。今天我們利用Tab Panel來建立一個擁有四個頁面的App。首先,我們先建立一個Tab Panel,在app.js里種入如下代碼:
- Ext.application({
- name: 'Sencha',
- launch: function() {// 應用啟動時執行該方法
- Ext.create("Ext.tab.Panel", {
- fullscreen: true,
- items: [
- {
- title: 'Home',
- iconCls: 'home',
- html: 'Welcome'
- }
- ]
- });
- }
- });
Ext.application({
name: 'Sencha',
launch: function() {// 應用啟動時執行該方法
Ext.create("Ext.tab.Panel", {
fullscreen: true,
items: [
{
title: 'Home',
iconCls: 'home',
html: 'Welcome'
}
]
});
}
});
保存后,可用支持HTML5的瀏覽器(我是chrome愛好者)打開app.html文件觀看效果,如下

現在,我們來改進一下這個頁面:
- launch: function() {
- Ext.create("Ext.tab.Panel", {
- fullscreen: true,
- tabBarPosition: 'bottom', // 將標簽欄定位到底部
- items: [
- {
- title: 'Home',
- iconCls: 'home',
- cls: 'home',// 添加了css class
- html: [
- '<img src="http://staging.sencha.com/img/sencha.png" />',
- '<h1>Welcome to Sencha Touch</h1>',
- "<p>You're creating the Getting Started app. This demonstrates how ",
- "to use tabs, lists and forms to create a simple app</p>",
- '<h2>Sencha Touch 2</h2>'
- ].join("")
- }
- ]
- });
- }
- });
launch: function() {
Ext.create("Ext.tab.Panel", {
fullscreen: true,
tabBarPosition: 'bottom', // 將標簽欄定位到底部
items: [
{
title: 'Home',
iconCls: 'home',
cls: 'home',// 添加了css class
html: [
'<img src="http://staging.sencha.com/img/sencha.png" />',
'<h1>Welcome to Sencha Touch</h1>',
"<p>You're creating the Getting Started app. This demonstrates how ",
"to use tabs, lists and forms to create a simple app</p>",
'<h2>Sencha Touch 2</h2>'
].join("")
}
]
});
}
});
打開css/style.css文件,輸入:
- .home {text-align:center;}
.home {text-align:center;}
然后,快快看看效果。
2.現在,讓我們來建立第二個頁面,blog頁面。我們可以選擇新建另一個js文件,然后修改app.html里面的引用;或者直接選擇覆蓋app.js:
- Ext.application({
- name: 'Sencha',
- launch: function() {
- Ext.create("Ext.tab.Panel", {
- fullscreen: true,
- tabBarPosition: 'bottom',
- items: [
- {
- xtype: 'nestedlist',// 這次建立一個嵌套列表(嵌套列表是什么,這里就不解釋了)
- title: 'Blog',
- iconCls: 'star',
- displayField: 'title',
- store: {// 這里是建立一個存放數據的data store,以后將對data store進行詳細介紹
- type: 'tree',
- fields: [
- 'title', 'link', 'author', 'contentSnippet', 'content',
- {name: 'leaf', defaultValue: true}
- ],
- root: {
- leaf: false
- },
- proxy: {// 我們使用ajax方式從google上獲取一些blog的數據,通過jsonp作為傳遞的載體,所以要聯網才能看到效果喔
- type: 'jsonp',
- url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog',
- reader: {// 這個是讀取數據的reader,數據取回來后通過reader轉成可被js認識的數據對象,我們要教會reader如何讀
- type: 'json',// 因為返回來的數據是json,我們要定義一個json reader
- rootProperty: 'responseData.feed.entries' // 將數據里面的entries節點當作根節點去讀取數據
- }
- }
- },
- detailCard: {// 建立一個用於顯示詳細內容的panel
- xtype: 'panel',
- scrollable: true,
- styleHtmlContent: true
- },
- listeners: {// 這里是監聽點擊列表某一項后所執行的方法
- itemtap: function(nestedList, list, index, element, post) {
- this.getDetailCard().setHtml(post.get('content'));
- }
- }
- }
- ]
- });
- }
- });
Ext.application({
name: 'Sencha',
launch: function() {
Ext.create("Ext.tab.Panel", {
fullscreen: true,
tabBarPosition: 'bottom',
items: [
{
xtype: 'nestedlist',// 這次建立一個嵌套列表(嵌套列表是什么,這里就不解釋了)
title: 'Blog',
iconCls: 'star',
displayField: 'title',
store: {// 這里是建立一個存放數據的data store,以后將對data store進行詳細介紹
type: 'tree',
fields: [
'title', 'link', 'author', 'contentSnippet', 'content',
{name: 'leaf', defaultValue: true}
],
root: {
leaf: false
},
proxy: {// 我們使用ajax方式從google上獲取一些blog的數據,通過jsonp作為傳遞的載體,所以要聯網才能看到效果喔
type: 'jsonp',
url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog',
reader: {// 這個是讀取數據的reader,數據取回來后通過reader轉成可被js認識的數據對象,我們要教會reader如何讀
type: 'json',// 因為返回來的數據是json,我們要定義一個json reader
rootProperty: 'responseData.feed.entries' // 將數據里面的entries節點當作根節點去讀取數據
}
}
},
detailCard: {// 建立一個用於顯示詳細內容的panel
xtype: 'panel',
scrollable: true,
styleHtmlContent: true
},
listeners: {// 這里是監聽點擊列表某一項后所執行的方法
itemtap: function(nestedList, list, index, element, post) {
this.getDetailCard().setHtml(post.get('content'));
}
}
}
]
});
}
});
種完沒?一起來看看效果:

點擊每一項后可以切換到詳細內容頁面。
3.完成了上面的工作,我們再來建立一個頁面,Contact頁面。種子如下,拿去種碼吧:
- Ext.application({
- name: 'Sencha',
- launch: function() {
- Ext.create("Ext.tab.Panel", {
- fullscreen: true,
- tabBarPosition: 'bottom',
- items: [
- {
- title: 'Contact',
- iconCls: 'user',
- xtype: 'formpanel',// 這次建立的是form panel
- url: 'contact.php',// 提交的action地址是contact.php。我們沒有這個文件,所以,就不要提交了。
- layout: 'vbox',
- items: [// 這里,我們有兩個成員
- {// 第一個成員是fieldset,將一些form元素包裝起來。
- xtype: 'fieldset',
- title: 'Contact Us',
- instructions: '(email address is optional)',
- items: [
- {
- xtype: 'textfield',// input text
- label: 'Name'
- },
- {
- xtype: 'emailfield',// input email,html5添加進來的新成員
- label: 'Email'
- },
- {
- xtype: 'textareafield',// textarea
- label: 'Message'
- }
- ]
- },
- {// 第二個成員,按鈕
- xtype: 'button',
- text: 'Send',
- ui: 'confirm',
- handler: function() {
- this.up('formpanel').submit();// 處理點擊事件的方法
- }
- }
- ]
- }
- ]
- });
- }
- });
Ext.application({
name: 'Sencha',
launch: function() {
Ext.create("Ext.tab.Panel", {
fullscreen: true,
tabBarPosition: 'bottom',
items: [
{
title: 'Contact',
iconCls: 'user',
xtype: 'formpanel',// 這次建立的是form panel
url: 'contact.php',// 提交的action地址是contact.php。我們沒有這個文件,所以,就不要提交了。
layout: 'vbox',
items: [// 這里,我們有兩個成員
{// 第一個成員是fieldset,將一些form元素包裝起來。
xtype: 'fieldset',
title: 'Contact Us',
instructions: '(email address is optional)',
items: [
{
xtype: 'textfield',// input text
label: 'Name'
},
{
xtype: 'emailfield',// input email,html5添加進來的新成員
label: 'Email'
},
{
xtype: 'textareafield',// textarea
label: 'Message'
}
]
},
{// 第二個成員,按鈕
xtype: 'button',
text: 'Send',
ui: 'confirm',
handler: function() {
this.up('formpanel').submit();// 處理點擊事件的方法
}
}
]
}
]
});
}
});
然后,上圖看真相:

4.合並。
三個欄目四個頁面大家都建立過了,也體驗過效果。可是,我們不是做一個app嗎?這樣怎么算一個。好了,現在我們將它們合並起來。
- Ext.application({
- name: 'Sencha',
- launch: function() {
- Ext.create("Ext.tab.Panel", {
- fullscreen: true,
- tabBarPosition: 'bottom',
- items: [// 這次,我們將三個欄目當成三個Tab Panel的成員
- {// 第一個成員,home頁面
- title: 'Home',
- iconCls: 'home',
- cls: 'home',
- html: [
- '<img width="65%" src="http://staging.sencha.com/img/sencha.png" />',
- '<h1>Welcome to Sencha Touch</h1>',
- "<p>You're creating the Getting Started app. This demonstrates how ",
- "to use tabs, lists and forms to create a simple app</p>",
- '<h2>Sencha Touch 2</h2>'
- ].join("")
- },
- {// 第二個成員,blog頁面
- xtype: 'nestedlist',
- title: 'Blog',
- iconCls: 'star',
- displayField: 'title',
- store: {
- type: 'tree',
- fields: [
- 'title', 'link', 'author', 'contentSnippet', 'content',
- {name: 'leaf', defaultValue: true}
- ],
- root: {
- leaf: false
- },
- proxy: {
- type: 'jsonp',
- url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog',
- reader: {
- type: 'json',
- rootProperty: 'responseData.feed.entries'
- }
- }
- },
- detailCard: {
- xtype: 'panel',
- scrollable: true,
- styleHtmlContent: true
- },
- listeners: {
- itemtap: function(nestedList, list, index, element, post) {
- this.getDetailCard().setHtml(post.get('content'));
- }
- }
- },
- {// 第三個成員,Contact頁面
- title: 'Contact',
- iconCls: 'user',
- xtype: 'formpanel',
- url: 'contact.php',
- layout: 'vbox',
- items: [
- {
- xtype: 'fieldset',
- title: 'Contact Us',
- instructions: '(email address is optional)',
- items: [
- {
- xtype: 'textfield',
- label: 'Name'
- },
- {
- xtype: 'emailfield',
- label: 'Email'
- },
- {
- xtype: 'textareafield',
- label: 'Message'
- }
- ]
- },
- {
- xtype: 'button',
- text: 'Send',
- ui: 'confirm',
- handler: function() {
- this.up('formpanel').submit();
- }
- }
- ]
- }
- ]
- });
- }
- });
Ext.application({
name: 'Sencha',
launch: function() {
Ext.create("Ext.tab.Panel", {
fullscreen: true,
tabBarPosition: 'bottom',
items: [// 這次,我們將三個欄目當成三個Tab Panel的成員
{// 第一個成員,home頁面
title: 'Home',
iconCls: 'home',
cls: 'home',
html: [
'<img width="65%" src="http://staging.sencha.com/img/sencha.png" />',
'<h1>Welcome to Sencha Touch</h1>',
"<p>You're creating the Getting Started app. This demonstrates how ",
"to use tabs, lists and forms to create a simple app</p>",
'<h2>Sencha Touch 2</h2>'
].join("")
},
{// 第二個成員,blog頁面
xtype: 'nestedlist',
title: 'Blog',
iconCls: 'star',
displayField: 'title',
store: {
type: 'tree',
fields: [
'title', 'link', 'author', 'contentSnippet', 'content',
{name: 'leaf', defaultValue: true}
],
root: {
leaf: false
},
proxy: {
type: 'jsonp',
url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog',
reader: {
type: 'json',
rootProperty: 'responseData.feed.entries'
}
}
},
detailCard: {
xtype: 'panel',
scrollable: true,
styleHtmlContent: true
},
listeners: {
itemtap: function(nestedList, list, index, element, post) {
this.getDetailCard().setHtml(post.get('content'));
}
}
},
{// 第三個成員,Contact頁面
title: 'Contact',
iconCls: 'user',
xtype: 'formpanel',
url: 'contact.php',
layout: 'vbox',
items: [
{
xtype: 'fieldset',
title: 'Contact Us',
instructions: '(email address is optional)',
items: [
{
xtype: 'textfield',
label: 'Name'
},
{
xtype: 'emailfield',
label: 'Email'
},
{
xtype: 'textareafield',
label: 'Message'
}
]
},
{
xtype: 'button',
text: 'Send',
ui: 'confirm',
handler: function() {
this.up('formpanel').submit();
}
}
]
}
]
});
}
});
趕快把程序跑起來,查看一下圖果吧。看是不是和下圖一樣?

這樣,我們很快就建立了一個基於HTML5的 Web App了。是不是很簡單?我們甚至可以用PhoneGap將它打包成 android或者iOS應用,發布到各自的應用商店去。至於PhoneGap,不在我們的討論范圍,在這里就不再詳談了。這次就介紹到這里。之后,我會給大家介紹Sencha Touch 2的詳細內容。
