-
10 Jan 2012 12:42 PM #1
Answered: Setting property values of items in parent class
Answered: Setting property values of items in parent class
Hi,
I have a Sencha touch class which extends Ext.Panel (let's call it: FilteredList). This class has a list component and a titlebar with a search field. The idea is to create a reusable list component with a header and a search field to filter the list.
I want to reuse this component around different apps by extending it. Now, I would like for a given child class to be able to set the title of my titlebar component (which is an item of my parent class FilteredList).
Is there a way I can do this while I'm defining my child class ?
Here is the code for the parent component:
Parent component
Thanks in advance for your help,Code:Ext.define('App.view.common.FilteredList' ,{ extend: 'Ext.Panel', alias: 'widget.filteredList', config: { items: [ { xtype: 'titlebar', id: 'navBar', //title: '', docked: 'top', items: [ { xtype: 'textfield', placeHolder: 'Search...', id: 'searchText' } ] }, { xtype: 'list', id: 'fList', flex: 1 } ] } });
Christian
-
Best Answer Posted by mitchellsimoens
First, I made some modifications of your abstract class:
I added the requires array which will tell Ext.Loader to load those files before this class is created and when you use SDK Tools, it will tell the build tool to have those required classes in the build before this class. I would get in the habit of this. Next I added layout : 'fit' so your list will be sized properly and added the title to null so that the setTitle, getTitle, applyTitle, and updateTitle are used. You can see the updateTitle gets the titlebar instance and does the setTitle on it.Code:Ext.define('App.view.common.FilteredList' ,{ extend : 'Ext.Panel', alias : 'widget.filteredList', requires : [ 'Ext.TitleBar', 'Ext.field.Text', 'Ext.dataview.List' ], config: { title : null, layout : 'fit', items : [ { xtype : 'titlebar', docked : 'top', items : [ { xtype : 'textfield', placeHolder : 'Search...' } ] }, { xtype : 'list' } ] }, updateTitle : function(title) { var titlebar = this.down('titlebar'); titlebar.setTitle(title); } });
So next is what if you want to extend this class just to change the title, it's as simple as this:
When a class is extended, the config objects are merged with the class that is extending another config object taking priority so the items are kept but the title is replaced by the one from UserList.Code:Ext.define('App.view.UserList', { extend : 'App.view.common.FilteredList', alias : 'widget.userlist', config : { title : 'Test Title' } });
-
11 Jan 2012 6:11 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
- Answers
- 3108
First, I made some modifications of your abstract class:
I added the requires array which will tell Ext.Loader to load those files before this class is created and when you use SDK Tools, it will tell the build tool to have those required classes in the build before this class. I would get in the habit of this. Next I added layout : 'fit' so your list will be sized properly and added the title to null so that the setTitle, getTitle, applyTitle, and updateTitle are used. You can see the updateTitle gets the titlebar instance and does the setTitle on it.Code:Ext.define('App.view.common.FilteredList' ,{ extend : 'Ext.Panel', alias : 'widget.filteredList', requires : [ 'Ext.TitleBar', 'Ext.field.Text', 'Ext.dataview.List' ], config: { title : null, layout : 'fit', items : [ { xtype : 'titlebar', docked : 'top', items : [ { xtype : 'textfield', placeHolder : 'Search...' } ] }, { xtype : 'list' } ] }, updateTitle : function(title) { var titlebar = this.down('titlebar'); titlebar.setTitle(title); } });
So next is what if you want to extend this class just to change the title, it's as simple as this:
When a class is extended, the config objects are merged with the class that is extending another config object taking priority so the items are kept but the title is replaced by the one from UserList.Code:Ext.define('App.view.UserList', { extend : 'App.view.common.FilteredList', alias : 'widget.userlist', config : { title : 'Test Title' } });Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
11 Jan 2012 5:08 PM #3
Thanks a lot for the very detailed answer !
I was thinking that config parameters could be overriden, but was not aware of this updateXXX method, which in the end allows us to update child components.
Christian


Reply With Quote