开始Sencha Touch 2之旅 -- Sencha Touch 官方指南系列

Sencha Touch是什么?

Sencha Touch (以下简称ST)能够帮助你快速地构造出基于HTML5的手机应用。通过ST构造出来的程序有着与原生应用一致的用户体验,目前已全面支持Android、IOS、黑莓这些主流平台。

准备工作

您不需要太多的准备。只需要:

  • 一个免费的Sencha Touch SDK。
  • 一个本机的web服务器。
  • 一个支持HTML5的浏览器(推荐使用Chrome和Safari)。

首先,下载最新的Sencha Touch SDK并解压至您的Web服务器根目录。如果您没有Web服务器,或者您不确定是否有,我们建议您安装一个简单的,如:WAMP和MAMP。

完成上述工作后,只需要用浏览器打开http://localhost/sencha-touch-folder,你就能看到ST的欢迎界面了。一切OK的话,我们就可以用ST来打造第一个应用程序了。

第一个程序

想让ST应用最高效地运行,请参照我们的示例进行开发。这是个约定,帮助我们写出易维护的程序,这在团队开发时尤其重要。

第一步,创建一个文件夹来存放您的应用程序。这里面你至少需要包含以下文件:

  • index.html - 一个简单的HTML文件,在里面引入ST框架以及您的应用程序文件。
  • app.js - 您的应用程序文件。定义您的主屏幕的图标和程序启动时所需要做的事情。
  • touch - ST框架文件的副本。

让我们先从index.html文件下手

复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Getting Started</title>
    <link rel="stylesheet" href="touch/resources/css/sencha-touch.css" type="text/css">
    <script type="text/javascript" src="touch/sencha-touch-all.js"></script>
    <script type="text/javascript" src="app.js"></script>
</head>
<body></body>
</html>
复制代码

这大概是您这辈子所写过的最简单的HTML页面了。它仅仅引入了ST(框架的js文件和css文件)和您自己的app.js。注意,body标签的内容为空 - ST将会自动帮我们渲染它。接下来,让我们来看看app.js文件中的内容。我们仍然从最简单的例子入手,只是弹出一个警告对话框:

复制代码
Ext.application({
    name: 'Sencha',

    launch: function() {
        alert('launched');
    }
});
复制代码

怎么样,简单吧。接下来让我们启动Safari(或者Chrome),看看他是否如我们预期的一样。(或者点击代码左侧的眼睛图标来查看运行的效果)。

到目前为止,这个程序什么也干不了。但警报消息的弹出意味着ST已经正确地被加载并运行了。我们开始着手构建界面:一个TabPanel。TabPanel是一个标签式的界面,他允许你在多个页面之间浏览。在这里,我们只生成一个页面 - 主页(Home page):

复制代码
Ext.application({
    name: 'Sencha',

    launch: function() {
        Ext.create("Ext.TabPanel", {
            fullscreen: true,
            items: [
                {
                    title: 'Home',
                    iconCls: 'home',
                    html: 'Welcome'
                }
            ]
        });
    }
});
复制代码

现在,TabPanel已经显示在屏幕上了,但是我们的主页,内容可以更丰富一些。把标签放在顶部有点不大好看,主页按钮似乎也有些单调。让我们动手吧,修改tabBarPosition配置项并添加一些HTML内容:

复制代码
Ext.application({
    name: 'Sencha',

    launch: function() {
        Ext.create("Ext.TabPanel", {
            fullscreen: true,
            tabBarPosition: 'bottom',

            items: [
                {
                    title: 'Home',
                    iconCls: 'home',
                    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.0.0pr1)</h2>'
                    ].join("")
                }
            ]
        });
    }
});
复制代码

到目前为止,我们已经有了一些HTML内容了,但不大好看(点击预览按钮查看代码示例)。为了使它更好看,我们只要给panel添加cls配置项即可。只是增加了一个CSS类,就搞定了。这里CSS是定义在examples/getting_started/app.css文件中的。添加了CSS之后,我们的主页就变成了这个样子:

复制代码
Ext.application({
    name: 'Sencha',

    launch: function() {
        Ext.create("Ext.TabPanel", {
            fullscreen: true,
            tabBarPosition: 'bottom',

            items: [
                {
                    title: 'Home',
                    iconCls: 'home',
                    cls: 'home',

                    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.0.0pr1)</h2>'
                    ].join("")
                }
            ]
        });
    }
});
复制代码

现在我们已经有了一个体面的主页,接下来我们稍微做一些扩展。我们希望在一个单独的选项卡显示我们的最新博客文章列表。我们先用虚拟的数据 - 在这里我选取了几个我最喜欢的来自http://sencha.com/blog的职位。关于List的代码我们写在"Home"选项卡下方。(点击预览按钮运行代码示例)

复制代码
Ext.application({
    name: 'Sencha',

    launch: function() {
        Ext.create("Ext.TabPanel", {
            fullscreen: true,
            tabBarPosition: 'bottom',

            items: [
                {
                    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.0.0pr1)</h2>'
                    ].join("")
                },
                {
                    xtype: 'list',
                    title: 'Blog',
                    iconCls: 'star',

                    itemTpl: '{title}',
                    store: {
                        fields: ['title', 'url'],
                        data: [
                            {title: 'Ext Scheduler 2.0', url: 'ext-scheduler-2-0-upgrading-to-ext-js-4'},
                            {title: 'Previewing Sencha Touch 2', url: 'sencha-touch-2-what-to-expect'},
                            {title: 'Sencha Con 2011', url: 'senchacon-2011-now-packed-with-more-goodness'},
                            {title: 'Documentation in Ext JS 4', url: 'new-ext-js-4-documentation-center'}
                        ]
                    }
                }
            ]
        }).setActiveItem(1);
    }
});
复制代码

 

我们回头再来添加点击这些List项的逻辑。文章的最后,我们再来添加一个联系表格。我们的最后一个tab,包含的是一个FromPanel和一个FieldSet:

复制代码
//We've added a third and final item to our tab panel - scroll down to see it
Ext.application({
    name: 'Sencha',

    launch: function() {
        Ext.create("Ext.TabPanel", {
            fullscreen: true,
            tabBarPosition: 'bottom',

            items: [
                {
                    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.0.0pr1)</h2>'
                    ].join("")
                },
                {
                    xtype: 'list',
                    title: 'Blog',
                    iconCls: 'star',

                    itemTpl: '{title}',
                    store: {
                        fields: ['title', 'url'],
                        data: [
                            {title: 'Ext Scheduler 2.0', url: 'ext-scheduler-2-0-upgrading-to-ext-js-4'},
                            {title: 'Previewing Sencha Touch 2', url: 'sencha-touch-2-what-to-expect'},
                            {title: 'Sencha Con 2011', url: 'senchacon-2011-now-packed-with-more-goodness'},
                            {title: 'Documentation in Ext JS 4', url: 'new-ext-js-4-documentation-center'}
                        ]
                    }
                },
                //this is the new item
                {
                    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();
                            }
                        }
                    ]
                }
            ]
        }).setActiveItem(2);
    }
});
复制代码

 

在第三个tab中,我们添加了一个表格,其中包含三个字段和一个提交按钮。我们使用了VBOX布局 布局来定位下方的提交按钮。fieldset本身配置了一个title 和一些 instructions(提示)。提示。最后,我们用了一个 textfield,一个emailfield 还有一个 textareafield.

你可以在examples/getting_started文件夹下找到本示例的文章代码。

更多内容

现在,我们已经搞定了一个非常基本的应用程序,是时候探索框架的其他内容了。在这里,我们提供一些开发指南和组件的实例供大家学习。并且随着Beta版的发展,我们也将会推出更多的指南用以构建较大规模的应用程序。

本文译自Sencha Touch 2 的官方指南。

翻译者:威老。

鉴于本人英语水平所限,难免会有些纰漏,欢迎大家指出。

转载请注明作者及原文地址,谢谢。


更多内容请关注 移动WEB开发社区

Sencha Touch 2 官方指南翻译帖 导航

转载请注明原文地址
作者: 威老
博客地址: http://www.cnblogs.com/weilao
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值