【问题标题】:Sencha Touch 2 - How to run a function after a view is loaded?Sencha Touch 2 - 加载视图后如何运行函数?
【发布时间】:2012-08-15 08:59:00
【问题描述】:

我在 Sencha Touch 中有一个视图,我通过初始化函数填充它。代码如下:

Ext.define("Pycsell.view.Home", {
    extend: "Ext.form.Panel",
    requires: "Ext.form.FieldSet",
    alias: "widget.homepage",
    config:{
        scrollable: false,
        cls: 'splash_screen',
        layout: 'vbox'
    },

    initialize: function () {
        this.callParent(arguments);

        var fb_login = {
            xtype: 'button',
            cls: 'fb_login_button'
        };

        var tradreg = {
            xtype: 'button',
            cls: 'tradreg_button'
        };

        var tradlog = {
            xtype: 'button',
            cls: 'tradlog_button'
        };

        var logo_container = Ext.create('Ext.Panel', {
            cls: 'logo_container',
            width: '90%',
            flex: 4,
        });

        var button_container = Ext.create('Ext.Panel', {
            cls: 'splash_content',
            items: [fb_login, tradreg, tradlog],
            flex: 2,
        });


        this.add([
            logo_container,
            button_container
        ]);
        this.setButtonSizes();

    },//End init

    setButtonSizes: function() {
        console.log('Width is:' + $('.fb_login_button').width());
        console.log('Old height is:' + $('.fb_login_button').height());
        var height = $('.fb_login_button').width()*0.29;
        $('.fb_login_button').height(height);
        console.log('New height is:' + $('.fb_login_button').height());
    }
});

现在,setButtonSizes 函数确实触发了,但所有值都为空,这让我相信这些项目在调用它时实际上并没有被初始化。我将如何正确执行此操作以便实际设置值?提前致谢。

【问题讨论】:

  • 有什么理由同时使用 jQuery 和 Sencha Touch ?
  • 老实说,我不确定我还能如何处理组件。
  • 你可以使用 Ext.getCmp('ID'); OR Ext.ComponentQuery.query('XTYPE') OR Ext.DomQuery.select('SELECTOR');
  • 这个问题在android领域?

标签: android jquery mobile touch extjs


【解决方案1】:

这可能是出了名的棘手。问题是您的函数可能在组件渲染之前被调用。我还没有对 Sencha Touch 2 做过任何工作,但看起来使用绘制事件应该会有所帮助。像这样的:

Ext.define("Pycsell.view.Home", {
 extend: "Ext.form.Panel",
 requires: "Ext.form.FieldSet",
 alias: "widget.homepage",
 config:{
     scrollable: false,
     cls: 'splash_screen',
     layout: 'vbox'
 },
  initialize: function () {
     this.callParent(arguments);
      var fb_login = {
         xtype: 'button',
         cls: 'fb_login_button'
     };
      var tradreg = {
         xtype: 'button',
         cls: 'tradreg_button'
     };
      var tradlog = {
         xtype: 'button',
         cls: 'tradlog_button'
     };
      var logo_container = Ext.create('Ext.Panel', {
         cls: 'logo_container',
         width: '90%',
         flex: 4,
     });
      var button_container = Ext.create('Ext.Panel', {
         cls: 'splash_content',
         items: [fb_login, tradreg, tradlog],
         flex: 2,
     });
       this.add([
         logo_container,
         button_container
     ]);
     /* Attaches a listener to the component that will be fired after the
     component is rendered or shown. */
     this.on('painted', this.setButtonSizes);
  },//End init
  setButtonSizes: function() {
     console.log('Width is:' + $('.fb_login_button').width());
     console.log('Old height is:' + $('.fb_login_button').height());
     var height = $('.fb_login_button').width()*0.29;      
     $('.fb_login_button').height(height);
     console.log('New height is:' + $('.fb_login_button').height());
 }
});

根据您要完成的任务,这可能会触发事件太多次(看起来每次显示组件时都会触发)。所以你可能需要调整 setButtonSizes 来补偿。

【讨论】:

  • 过会儿试试,听起来不错。我想要完成的是动态调整屏幕上按钮的大小,使它们符合屏幕的 % 大小,而不是固定的 px 值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
相关资源
最近更新 更多