建立第一个Sencha Touch应用
准备
开始开发前,请先到下面的地址下载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的详细内容。
建立第一个Sencha Touch应用的更多相关文章
- sencha touch建立新项目
首先你得有一个sencha touch的环境,如下图: 图1 touch的sdk环境 有了这个之后,通过在cmd中执行下列命令: sencha -sdk /path/to/framework gene ...
- 【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之三
原文:Getting Started with Sencha Touch 2: Build a Weather Utility App (Part 3) 作者:Lee BoonstraLee is a ...
- Android环境配置Sencha Touch
转自http://www.phonegap100.com/portal.php?mod=view&aid=19 作为你开发的一部分,为安卓设备开发的 Sencha Touch框架应该在安卓虚拟 ...
- 用 Sencha Touch 构建移动 web 应用程序
Sencha Touch 是一个使用 HTML5.CSS3 和 JavaScript 语言构建的移动 web 应用程序框架,在本文中,学习如何应用您当前的 web 开发技能进行移动 web 开发.下载 ...
- sencha touch打包成安装程序
为了更好地向大家演示如何打包一个sencha touch的项目,我们用sencha cmd创建一个演示项目,如果你的sencha cmd环境还没有配置,请参照 sencha touch 入门系列 (二 ...
- sencha touch 入门系列 (二)sencha touch 开发准备
这是本人第一次写博客教程,没什么经验,文笔也不是很好,写这教程一方面为了巩固自己这段时间的学习成果,一方面帮助大家解决问题,欢迎大家多提建议,指出问题.接下来我们就开始我们的sencha touch开 ...
- sencha touch 开发准备
这是本人第一次写博客教程,没什么经验,文笔也不是很好,写这教程一方面为了巩固自己这段时间的学习成果,一方面帮助大家解决问题,欢迎大家多提建议,指出问题.接下来我们就开始我们的sencha touch开 ...
- sencha touch tabsidebar 源码扩展
先上图看效果 没错,这是一个sencha touch 项目,而这里的右边推出效果(下文叫做tabsiderbar),使用插件tabsiderbar来扩展的. 插件js下载地址:http://www.m ...
- 【翻译】在Sencha Touch中创建离线/在线代理
原文:Creating an Online/Offline proxy in Sencha Touch 概述 在Sencha Touch中,一个常见的需求就是,当设备在没有连接互联网的时候,应用程序必 ...
随机推荐
- Python字典类型、
字典类型: # msg_dic = {# 'apple': 10,# 'tesla': 100000,# 'mac': 3000,# 'lenovo': 30000,# ...
- 用CSV库一行行插入数据
语料团队之前都是手动标注文字的定位位置,今天写了个小脚本,帮他们批量生成文字对应的定位. 其中数据生成后,要生成csv文件,查看了下使用csv库. import csv row1 = [1,2,3,4 ...
- Tyk-Hybrid模式安装—抽象方法论,重用它
最近,公司有计划运用API网关.那么,在经过权衡之后,使用了Tyk的Hybrid模式!现在环境没问题了,API调用也测通了.我得想想合并服务,监控API实时情况的东西.但在这个环境搭建的过程中,我目前 ...
- 【转】深入JVM系列(一)之内存模型与内存分配
http://lovnet.iteye.com/blog/1825324 一.JVM内存区域划分 大多数 JVM 将内存区域划分为 Method Area(Non-Heap),Heap,Progr ...
- Educational Codeforces Round 10——B. z-sort
B. z-sort time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- Codeforces834D - The Bakery
Portal Description 给出一个\(n(n\leq35000)\)个数的数列\(\{a_i\}\)和\(m(m\leq50)\).将原数列划分成\(m\)个连续的部分,每个部分的权值等于 ...
- 刷题总结——影魔(HNOI2017 BZOJ4826 线段树+扫描线)
题目: Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样 的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄 ...
- scrapy怎么设置带有密码的代理ip base64.encodestring不能用 python3.5,base64库里面的encodestring()被换成了什么?
自己写爬虫时买的代理ip有密码,在网上查了都是下面这种: 1.在Scrapy工程下新建"middlewares.py": import base64 # Start your mi ...
- 2018.8.8 Noip2018模拟测试赛(二十一)
日期: 八月七号 总分: 300分 难度: 提高 ~ 省选 得分: 112分(OvO) 题目目录: T1:幸福的道路 T2:Solitaire T3:Flags 赛后心得: 第一题裸树d啊! ...
- Android上下文Context
Android上下文Context介绍 在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service.BroadcastReceiv ...