PDA

View Full Version : Getting a panel data from a button inside it



gostbuster
7 Sep 2012, 8:03 AM
I everyone, this seems to be simple but I don't succeed to get panel's data from a button. Showing code will try to explain :



Ext.define('MyPanel',{
extends:'Ext.Panel'
config:{
data:{ number:123456}
items:[
{xtype:'button', action="myaction"}
]

}

})


I'm working with MVC so I created a controler, and set my refs and controls on tap. So, I have the following function



onMyaction:function(button) { //yes it can take the button reference

var num = // I want here my number (123456)

}


I tried scope:this in order to do later a button.getData(); but this doesn't work.

the only solution I found is doing this :



onMyaction:function(button) { //yes it can take the button reference
var num =button.getParent().getData().number // This works but looks messy....
}

(The problem is, is that if I wanna put my button inside a toolbar, I would have to change my code and do a callParent().callParent() which looks crazy....:-?
Isn't messy (dirty) ? Does a better way exists to do what I wish ? I'm convinced that yes...but humm...I don't know..

Thank you very much in advance.

bryan641
7 Sep 2012, 2:18 PM
Disclaimer: I'm very new to Sencha Touch and MVC programming.

The way I did this was to create xtypes for the form elements I needed to access (maybe you could just do it for the form and get to the components of it that way).

So my text field was defined along with the view like this:

Ext.define('MyApp.view.Main.emailAddr', {
extend: 'Ext.field.Text',
xtype: 'emailaddr'.
...
});

Then added an entry in the "refs" section of my controller:

Ext.define('MyApp.controller.Main', {
extend: 'Ext.app.Controller',
config: {
refs: {
emailAddr: 'emailaddr',
...

and in the function in my controller, I got the value like this:


submitEmail: function() {
var e = this.getEmailAddr();
var em = e.getValue();

--Bryan

gostbuster
19 Sep 2012, 6:21 AM
Sure, but you're in a controller. I'm not, I'm in the view itself...

bryan641
19 Sep 2012, 6:32 AM
Sorry, I thought from this that you were in the controller:

I'm working with MVC so I created a controler, and set my refs and controls on tap.

Personally, I don't know why you wouldn't put your actions in the controller. But the only other method I've tried (earlier, before I started using MVC more effectively) was to assign an ID to the element and use Ext.get('idname').

As I recall, the ID was actually assigned to a div above my form element, so you might need to experiment a bit to get to the actual data you need.

--Bryan

--Bryan