PDA

View Full Version : Event handling in MVC text fields?



gardner
1 May 2012, 4:05 AM
I am unclear on the preferred method of event handling when trying to apply an MVC model. The button examples show a button configured like this:

View item:


{
text: 'Save',
action: 'save'
}

Controller code:


'useredit button[action=save]': {
click: this.updateUser
}

Which matches the concept of pushing all logic out of the view into the controller. The text field examples, however, look like this, with everything in the view:


{
fieldLabel: 'Field 2',
name: 'field2',
listeners: {
specialkey: function(field, e) {
if( e.getKey() == e.ENTER) {
var form = field.up('form').getForm();
form.submit();
}
}
}


This lets you handle the different events differently, but leaves teh code in the view. Is this accepted practice? One of my developers has tried this approach for text fields, but I am not sure I am comfortable with it:

View item:


{
xtype: 'textfield',
fieldLabel: 'Phone',
name: 'phone',
itemId: 'phone',
action: 'hitEnter'
}

Controller code:


{
'targetForm textfield[action=hitEnter]' : {
specialkey: this.hitEnter
}

In a related question, I have not found documentation on exactly how you specify actions. Can you just put in any string and use it as in the example above?

Thanks. I am a newbie on ExtJS, so no reply is too basic.

- Gardner

mitchellsimoens
1 May 2012, 5:34 AM
We don't force people to use MVC so you can use listeners on the view itself but all those listeners are available to be listened to in the controller.

gardner
1 May 2012, 6:22 AM
Mitchell,

Thanks for the reply, but I am not sure if you mean that they last code example is the preferred method, or if we should explicitly have some sort of listener code in the controller. Is the "action" keyword in the view what we are supposed to do for all events in MVC?

A code fragment would probably be helpful.

- Gardner

scottmartin
17 May 2012, 4:34 PM
MVC is for you to organize your code. If you want to use the MVC convention, then use the controller for your event handling to maintain our pattern.

This does not mean you are forced to and can certainly place a listener in your view if you prefer.

Regards,
Scott.

nhan
19 Sep 2012, 3:30 PM
Does anyone have examples on this? I have multiple pages. How do I pass an event to the controller level and how one page listens to another page's event?
Thanks,
nhan