1. #1
    Sencha User
    Join Date
    Jul 2012
    Location
    Italy
    Posts
    84
    Answers
    1
    Vote Rating
    4
    Tegola is on a distinguished road

      0  

    Default Answered: Best way to create a custom component

    Answered: Best way to create a custom component


    Hello,
    I'm creating a complicate custom component. It's a timer which has a display (to show the current time), a play/pause, a remove button and a few other things inside.

    I'm actually creating the component by nesting components and adding events on initialization, but I'm not sure this is the best way to do it, and since there may be 8/10 of these complicate components, I'd like to know how to optimize it.

    Here's my current component. It's still in early stage:

    Code:
    Ext.define('My.view.Timer', {
    	extend: 'Ext.Container',
    	xtype: 'timer',
    	
    	config: {
    		baseCls: 'my-timer',
    		layout: 'vbox',
    		defaults: { xtype: 'component' },
    		items: [
    			{ // First section
    				xtype: 'container',
    				layout: 'hbox',
    				defaults: {
    					xtype: 'button',
    					baseCls: 'my-timer-button'
    				},
    				items: [
    					{
    						cls: 'my-timer-play-button',
    						iconCls: 'play',
    						itemId: 'playButton'
    					},
    					{
    						xtype: 'component',
    						baseCls: 'my-timer-display',
    						itemId: 'display',
    						flex: 1,
    						tpl: [
    							'<div class="field">',
    								'<div class="value">{hours}</div>',
    								'<div class="label">Hr</div>',
    							'</div>',
    							'<div class="field colon-field">',
    								'<div class="value">:</div>',
    								'<div class="label">&nbsp;</div>',
    							'</div>',
    							'<div class="field">',
    								'<div class="value">{minutes}</div>',
    								'<div class="label">Min</div>',
    							'</div>',
    							'<div class="field colon-field">',
    								'<div class="value">:</div>',
    								'<div class="label">&nbsp;</div>',
    							'</div>',
    							'<div class="field">',
    								'<div class="value">{seconds}</div>',
    								'<div class="label">Sec</div>',
    							'</div>',
    						]
    					},
    					{
    						cls: 'my-timer-save-button',
    						iconCls: 'save',
    						itemId: 'saveButton'
    					},
    					{
    						cls: 'my-timer-delete-button',
    						iconCls: 'delete',
    						itemId: 'deleteButton'
    					}
    				]
    			},
    			{ // Separator
    				baseCls: 'my-timer-separator'
    			},
    			{ // Description
    				baseCls: 'my-timer-description',
    				html: 'Timer description',
    				itemId: 'descriptionField'
    			}
    		
    		]
    	},
    	
    	initialize: function() {
    		this.callParent(arguments);
    		
    		// Add listeners to elements
    		this.down('#playButton').on('tap', this.playPause, this);
    		this.down('#saveButton').on('tap', this.save, this);
    		this.down('#deleteButton').on('tap', this.delete, this);
    	},
    	
    	updateData: function(data, oldData) {
    		this.callParent(arguments);
    		
    		if (data) {
    			this.down('#display').setData({
    				hours: Ext.util.Format.leftPad(data.hours, 2, '0'),
    				minutes: Ext.util.Format.leftPad(data.minutes, 2, '0'),
    				seconds: Ext.util.Format.leftPad(data.seconds, 2, '0')
    			});
    		}
    	},
    	
    	playPause: function() {
    		this.fireEvent('playPause', this);
    	}
    	
    	save: function() {
    		this.fireEvent('save', this);
    	},
    
    
    	delete: function() {
    		this.fireEvent('delete', this);
    	},
    });
    Is this the right/best way?
    Thanks

  2. What you have will work but you could also add this to the config object instead of the on methods:

    Code:
    control : {
        '#playButton' : {
            tap : 'playPause'
        },
        '#saveButton' : {
            tap : 'save'
        },
        '#deleteButton' : {
            tap : 'delete'
        }
    }

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,811
    Answers
    3467
    Vote Rating
    834
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    What you have will work but you could also add this to the config object instead of the on methods:

    Code:
    control : {
        '#playButton' : {
            tap : 'playPause'
        },
        '#saveButton' : {
            tap : 'save'
        },
        '#deleteButton' : {
            tap : 'delete'
        }
    }
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Jul 2012
    Location
    Italy
    Posts
    84
    Answers
    1
    Vote Rating
    4
    Tegola is on a distinguished road

      0  

    Default


    I didn't know I could use the control config inside a view. Neat!

    Thanks!

  5. #4
    Sencha User
    Join Date
    Jul 2012
    Location
    Italy
    Posts
    84
    Answers
    1
    Vote Rating
    4
    Tegola is on a distinguished road

      0  

    Default


    Is there also a way to mimick the "refs" config?
    In self contained components I'm finding myself referencing to child components, and I'm actually using "this.down(...)" to find them.

    I know I could do something like:
    Code:
    initialize: {
      this.callParent(arguments);
    
      this.myButton = this.down('#myButton');
    }
    but any events controller by the "control" config would fire before the "initialize" method, thus not finding myButton.

    Thanks

Thread Participants: 1

Tags for this Thread