PDA

View Full Version : A few good practice questions



IAmAndre
30 Mar 2014, 11:20 AM
Hi,

I started developing an application and I'm not satisfied by the way the code looks. Here are my questions :
- I currently have a few forms on my main page and there is a lot of code nested in, with handlers. For example :

buttons: [{
text: 'Submit',
margin: '0 100 0 0',
formBind: true,
handler: function(){
this.up('form').getForm().updateRecord();
var record = this.up('form').getForm().getRecord();
Ext.Ajax.request({
url: 'http://localholes fait diversst/ext/test/login.php',
method: 'POST',
params: {
'username':record.Username,
'password':record.Password
},
success: function(response,options){
// Ext.Msg.alert('Test', response.responseText);
var info = Ext.JSON.decode(response.responseText);
alert(info.status);
}
});
}
}]
This code works fine, but I want to know if there is a way to write it somewhere else. In this case, should I declare the button as a View, or is there a way to do it with a Controller?

- My other question is quite similar, but about this code

Ext.application({
name: 'HelloExt',
launch: function() {

So basically I am a bit lost about how to structure my code and how and when the controllers should be used. I hope all that makes sense.

Thanks in advance for your replies.

israelroldan
2 Apr 2014, 11:52 AM
The first part sounds like a task for the controller's 'control' block...
Take a look at the MVC Application Architecture guide in the docs: http://docs-origin.sencha.com/extjs/4.2.2/#!/guide/application_architecture I'm pretty sure you'll find the answers to both your questions there :).

alexsai
3 Apr 2014, 4:43 AM
Definitely you have to use the MVC pattern.
You can define a controller for manage events like this and reference it into application.js


Ext.define('APPNAME.controller.Main', { extend: 'Ext.app.Controller',
views:[
'YoutLogInView'
],
init:function(){
this.control({
'YoutLogInView #idOfTheLoginButton' : {
click : this.onLogin
},
'accessform #nullfilter' :{
change : this.onNullFilter
}
});
},
onLogin: function( that, newValue, oldValue, eOpts ){
// AJAX CALL
}
});