0

我有两种看法。用户视图有一些文本和一个按钮。我想使用该按钮切换到第二个视图。但我不知道这如何与 sencha touch 2 一起使用。当我按下“UserView”(这是第一个视图)上的按钮时,我收到以下错误:

未捕获的错误:SYNTAX_ERR:DOM 异常 12

这基本上就是我的代码现在的样子:

应用程序.js

Ext.Loader.setConfig({ enabled: true });

Ext.setup({
    viewport: {
        autoMaximize: false
    },
    onReady: function() {
        var app = new Ext.Application({
            name: 'AM',
            controllers: [
                'Main'
            ]
        });
    }
});

主控制器

Ext.define('AM.controller.Main', {
    extend: 'Ext.app.Controller',
    views : ['User'],
    init: function() {
        this.getUserView().create();

        this.control ({
            '#new': {
               tap: function() {
                    alert('aaaa');
               }
            }
        });
    }
}); 

以及两种观点:

Ext.define('AM.view.User', {
    extend: 'Ext.Container',
    config: {
        fullscreen:true,
        items: [{
                xtype: 'button',
                text: 'New',
                id: 'new'
            }
        ],
        html: 'Testing<br />'
    }
}); 

第二视图

Ext.define('AM.view.New', {
    extend: 'Ext.Container',
    config: {
        fullscreen: true,
        html: 'w00t'
    }
});
4

1 回答 1

8

这是您的应用程序以应有的方式编写的。以下是关于我所做更改的一些说明:

  • 您应该在创建 MVC 应用程序时调用Ext.application而不是调用。Ext.setup
  • 所有根视图都应该在你的app.js中定义。
  • 您不再需要this.control()在控制器中使用。您可以简单地使用配置块中的控制配置。
  • 您应该viewsExt.application.
  • 与其在 中创建视图init,不如在 中创建视图launch。这是组件应该添加到视图中。

应用程序/视图/User.js

Ext.define('AM.view.User', {
    extend: 'Ext.Container',

    config: {
        items: [
            {
                xtype: 'button',
                text: 'New',
                id: 'new'
            }
        ],
        html: 'Testing<br />'
    }
});

应用程序/视图/New.js

Ext.define('AM.view.New', {
    extend: 'Ext.Container',
    config: {
        html: 'w00t'
    }
});

应用程序/控制器/Main.js

Ext.define('AM.controller.Main', {
    extend: 'Ext.app.Controller',

    config: {
        control: {
            '#new': {
                // On the tap event, call onNewTap
                tap: 'onNewTap'
            }
        }
    },

    launch: function() {
        // When our controller is launched, create an instance of our User view and add it to the viewport
        // which has a card layout
        Ext.Viewport.add(Ext.create('AM.view.User'));
    },

    onNewTap: function() {
        // When the user taps on the button, create a new reference of our New view, and set it as the active
        // item of Ext.Viewport
        Ext.Viewport.setActiveItem(Ext.create('AM.view.New'));
    }
});

应用程序.js

Ext.application({
    name: 'AM',

    // Include the only controller
    controllers: ['Main'],

    // Include all views
    views: ['User', 'New'],

    // Give the Ext.Viewport global instance a custom layout and animation
    viewport: {
        layout: {
            type: 'card',
            animation: {
                type: 'slide',
                direction: 'left',
                duration: 300
            }
        }
    }
});

我还建议您查看Sencha Touch API Docs上的精彩指南,以及查看Sencha 论坛,因为它们非常活跃并且是一个很好的信息来源。

于 2012-02-13T01:53:49.617 回答