14

我是 sencha touch 2.0 的新手。我有一个 html 文件。我正在尝试将此 html 文件(或内容)加载到面板中。我只是在使用 ajax 调用,但它不起作用。以下是代码。

这是我在浏览器中运行的 html 文件。

索引.html

<script type="text/javascript" src="touch/sencha-touch-debug.js"></script>
<script type="text/javascript" src="HTMLPanel.js"></script>
<script type="text/javascript" src="app.js"></script>

这是app.js

Ext.setup({
    name : 'SampleLoad',
    requires : ['HTMLPanel'],
    launch : function () {
        var HTMLPanel = new HTMLPanel({
            scroll : 'vertical',
            title : 'My HTML Panel',
            url : 'sample.html'
        });
    }
});

这是HTMLPanel.js

//HTMLPanel = Ext.extend(Ext.Panel, { //gives error 
var HTMLPanel = Ext.define('HTMLPanel',{
    extend : 'Ext.Panel',
    constructor : function( config ) {
        HTMLPanel.superclass.constructor.apply(this, arguments);

        // load the html file with ajax when the item is
        // added to the parent container
        this.on(
            "activate",
            function( panel, container, index ) {
                if( this.url && (this.url.length > 0) )
                {
                    Ext.Ajax.request({
                        url : this.url,
                        method : "GET",
                        success : function( response, request ) {
                            //console.log("success -- response: "+response.responseText);
                            panel.update(response.responseText);
                        },
                        failure : function( response, request ) {
                            //console.log("failed -- response: "+response.responseText);
                        }
                    });
                }
            },
            this
        )
    },

    url : null
});

我只想在面板中显示 html 内容。有人可以帮忙吗?

4

2 回答 2

31

Sencha Touch 2 中的职业系统与 1.x 相比发生了很大变化。它现在与 ExtJS 4 非常相似。更改背后的想法是使其更易于理解、更快地开发和更具活力。

一些变化:

  • 你不应该再使用new HTMLPanel({}). 相反,使用Ext.create,即Ext.create('HTMLPanel', {})
  • 你不应该再用它Ext.extend来定义你的自定义类。相反,Ext.defineextend属性一起使用。
  • 您不再需要使用HTMLPanel.superclass.constrctor.apply(this, arguments)来调用父级。相反,您可以使用this.callParent(arguments)
  • 你应该很少覆盖constructor. 这是不好的做法。相反,您应该使用该config块:

    Ext.define('HTMLPanel', {
        extend: 'Ext.Panel',
    
        config: {
            html: 'This is the html of this panel.'
        }
    });
    

    请注意,配置仅config在您使用Ext.define. 如果您使用或使用具有 xtype 的对象创建类的新实例Ext.create,则配置不需要位于 config 对象中。new ClassName

您可以通过阅读本指南了解有关新班级系统的更多信息。这里还有一个关于如何从 1.x 迁移到 2.x 的很好的指南

所以,让你的代码工作。

index.html(无需更改):

<script type="text/javascript" src="touch/sencha-touch-debug.js"></script>
<script type="text/javascript" src="HTMLPanel.js"></script>
<script type="text/javascript" src="app.js"></script>

应用程序.js

// You should use Ext.application, not Ext.setup
Ext.application({
    name: 'SampleLoad',
    requires: ['HTMLPanel'],
    launch: function () {
        var HTMLPanel = Ext.create('HTMLPanel', {
            // this is now `scrollable`, not `scroll`
            //scroll: 'vertical',
            scrollable: 'vertical',

            url: 'sample.html'
        });

        // Add the new HTMLPanel into the viewport so it is visible
        Ext.Viewport.add(HTMLPanel);
    }
});

HTMLPanel.js

// You do not need to save a reference to HTMLPanel when you define your class
//var HTMLPanel = Ext.define('HTMLPanel', {
Ext.define('HTMLPanel', {
    extend: 'Ext.Panel',

    // We are using Ext.Ajax, so we should require it
    requires: ['Ext.Ajax'],

    config: {
        listeners: {
            activate: 'onActivate'
        },

        // Create a new configuration called `url` so we can specify the URL
        url: null
    },

    onActivate: function(me, container) {
        Ext.Ajax.request({
            // we should use the getter for our new `url` config
            url: this.getUrl(),
            method: "GET",
            success: function(response, request) {
                // We should use the setter for the HTML config for this
                me.setHtml(response.responseText);
            },
            failure: function(response, request) {
                me.setHtml("failed -- response: " + response.responseText);
            }
        });
    }
});

希望这会有所帮助。

于 2012-02-12T21:01:44.290 回答
5

rdougan的答案对我有用。如果它仍然不适合您,请查看Sencha Docs 中的这个示例,他们使用 AJAX 加载 .js 文件的方式略有不同(对于 .html 文件来说完全一样)。要获取源代码,请下载 Sencha Touch 2 SDK,它将位于examples/nestedlist.

于 2012-06-12T22:57:53.973 回答