将html文件加载到面板中 [英] load an html file into a panel

查看:14
本文介绍了将html文件加载到面板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用Sencha Touch 2.0。我有一个超文本标记语言文件。我正在尝试将此html文件(或内容)加载到面板中。我只是使用了一个AJAX调用,但它不起作用。以下是代码。

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

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>

这是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内容。有人能帮忙吗?

推荐答案

与1.x相比,Sencha Touch2中的职业系统发生了很大变化。它现在非常类似于ExtJS4。这些变化背后的理念是让它更容易理解、更快地开发和更有活力。

部分更改:

  • 您不应再使用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.'
        }
    });
    
    请注意,当您使用Ext.define定义新类时,配置仅位于config块中。如果要使用Ext.createnew ClassName或使用具有xtype的对象创建类的新实例,不需要位于配置对象中。

您可以通过阅读this guide了解有关新类系统的更多信息。还提供了如何从1.x迁移到2.xhere的很好的指南。

那么,让我们让您的代码正常工作。

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>

app.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);
            }
        });
    }
});

希望这能有所帮助。

这篇关于将html文件加载到面板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆