Adding a 'Controller' class to my MVC based Sencha Touch project

*爱你&永不变心* 提交于 2019-12-11 08:19:16

问题


I have created a MVC SenchaTouch project. I have created a sample application where the user will enter the username and password and send it to the web service.

The username, and password fields are suppose to be in the 'VIEW', and the 'sending it to the web service' functionality should be in the 'CONTROLLER'.

In my program, the sending to the web service part is also there in the 'VIEW', so can someone please help me edit my code, so that i could have the 'send to the web service' functionality in the 'CONTROLLER'

Here's the code

Ext.define('TU.view.Contact',{
           extend:'Ext.form.Panel',
           xtype:'contactform',

           config: {

           title:'Contact',
           iconCls:'user',

           url:'http://somesite.com/contact.php',
           items: [
           {
                   xtype:'fieldset',
                   title:'User Login',

                   items:[
                          {
                          xtype:'textfield',
                          name:'name',
                          label:'Name'
                          },
                          {
                          xtype:'passwordfield',
                          name:'password',
                          label:'Password'
                          }
                          ]
           },
           {
                   xtype:'button',
                   text:'Send',
                   ui:'confirm',
                   padding:5,
                   handler:function(){
                   this.up(contactform).submit();

                   }
           }
           ]



           }




           });

回答1:


Firstly, set the id property for your button.

 xtype:'button',
 id:'submitBtn',
 text:'Send'
 .....
 .....

In the controller class,

Ext.define('MyApp.controller.Main', {
    extend: 'Ext.app.Controller',

    config: {
        refs : {
            submitBtn: '#submitBtn'
        },

        control : {
            submitBtn: {
                tap: 'submitData'
            }
        },
    },

    submitData: function() {
             var form = Ext.getCmp('form-id');

             // Get username and password fields ...
             var formvalues = form.getValues();

             // Web Service code goes here ..
             Ext.Ajax.request({
                 params: formvalues,                     
                 url:'http://_servername_.com/app/insert.php'

                 success : function() {
                       Ext.Msg.alert('Success','Username and password successfully entered');
                 }

                 failure : function() {
                       Ext.Msg.alert('Failure','Error while adding data');
                 }
             });        
    }
});

NOTE : insert.php will be the server side code ( just an e.g) that will make a connection with your database and enter the values in it.



来源:https://stackoverflow.com/questions/10463835/adding-a-controller-class-to-my-mvc-based-sencha-touch-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!