-
8 Oct 2012 5:03 AM #1
Unanswered: Add own button with handler to navigation bar
Unanswered: Add own button with handler to navigation bar
Hi,
I've already read a lot of posts, but couldn't find a complete example of adding a button to a navigation bar.
My code so far is this:
The handler doesn't work. I've also tried with 'listeners' but the event gets never fired.Code:Ext.define('LeaveRequest.view.MyRequests', { extend : 'Ext.navigation.View', alias : 'widget.MyRequests', requires : [...], config : { title : 'My Requests', fullscreen : true, items : [...], navigationBar : { items : [ { id : 'addRequest', xtype : 'button', iconCls : 'add', iconMask : true, align : 'right', scope:this, handler:this.addRequestButton } ] } }, addRequestButton : function() { console.log('request button') ; this.fireEvent('addRequestButton', this); } });
On another thread i saw this
when i place this in the initialize function, the back button of the navigationview won't work anymore.Code:Ext.ComponentQuery.query('navigationview')[0].getNavigationBar().add( { xtype : 'button', iconCls : 'add', iconMask : true, align : 'right', scope:this, handler: this.addRequestButton });
can somebody help please?
-
8 Oct 2012 9:51 AM #2Sencha Premium Member
- Join Date
- Feb 2012
- Location
- Berne, Switzerland
- Posts
- 581
- Vote Rating
- 32
- Answers
- 34
First thing to note is that the function and the event you fire have the same name! This can't work.
Are you using MVC?
-
8 Oct 2012 10:28 AM #3
Yes i'm using MVC. I use the same names in my other classes for the handler en the fire event and it still works.
The log in the function never shows in the console, so the problem is something else.
-
8 Oct 2012 10:35 AM #4Sencha Premium Member
- Join Date
- Feb 2012
- Location
- Berne, Switzerland
- Posts
- 581
- Vote Rating
- 32
- Answers
- 34
In MVC you have controllers. So why don't you listen for your button in the controller? The way you are doing this seems a bit complicated to me...
-
8 Oct 2012 10:43 AM #5
It's better to fire an event to the controller than let the controller listen to a possible event.
This way, i can change whatever i want in the view and the controller will still work.
it's action based instead of polled.
i use this technique everywhere and it works perfectly. only not with a button within the navigation bar.
-
8 Oct 2012 11:15 AM #6
I do not know why you have to use navigation view.
Why not use toolbar/titlebar and card layout?I write English by translator.
-
8 Oct 2012 11:19 AM #7
i really need navigationview for the back buttons. these are created dynamically when you push new views.
-
8 Oct 2012 11:57 AM #8
ok.Your first method:
the 'this' is window object ,because read contextCode:{ id : 'addRequest', xtype : 'button', iconCls : 'add', iconMask : true, align : 'right', scope:this, handler:this.addRequestButton }
Your second method:Code:// here is scope: window Ext.define('',{/* here is a anonymous object create by window*/}); //more explain var configObj={ scope: this// this is window,you know? } console.info(configObj.scope);// will print window object. Ext.define('',configObj);//do you understand?
when i place this in the initialize function, the back button of the navigationview won't work anymore.Code:Ext.define('LeaveRequest.view.MyRequests', { extend : 'Ext.navigation.View', xtype:'myrequests', config : { title : 'My Requests', fullscreen : true, items : [{html:'views',title:'the title'}] }, initialize: function(){ this.callParent();//i think you missed callparent. this.getNavigationBar().add({ id : 'addRequest', xtype : 'button', iconCls : 'add', iconMask : true, align : 'right', scope:this, handler:this.addRequestButton }); }, addRequestButton : function() { console.log('request button') ; this.push({ title:'second', html:'other view' }) this.fireEvent('addRequestButton', this); } });Last edited by haduki; 8 Oct 2012 at 12:35 PM. Reason: add explanation
I write English by translator.
-
8 Oct 2012 12:24 PM #9
Could you give an explanation please?
Not sure what you mean with that code.
-
8 Oct 2012 12:32 PM #10
I just tried it with the callParent and it works, thank you very much!


Reply With Quote