Question

I have two views. The User view has some text and a button. I want to use that button to switch to the second view. But i don't know how this works with sencha touch 2. When i press the button on the "UserView" (which is the first view), then i get the following error:

Uncaught Error: SYNTAX_ERR: DOM Exception 12

This is basically how my code looks right now:

app.js

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

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

The Main controller

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

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

And the two views:

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

2nd view

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

Solution

Here is your application written the way it should be. Here are a few notes about the changes I made:

  • You should call Ext.application instead of Ext.setup when creating your MVC application.
  • All root views should be defined inside your app.js.
  • You no longer need to use this.control() in your controllers. You can simply use the control configuration in the config block.
  • You should define all views in the views config of Ext.application.
  • Instead of creating the view in init, do it in launch. This is components should be added into the view.

app/view/User.js

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

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

app/view/New.js

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

app/controller/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'));
    }
});

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

I also suggest you checkout the great guides over on the Sencha Touch API Docs, as well as checking out the Sencha Forums as they are very active and are a great source of information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top