1

我创建了一个 MVC SenchaTouch 项目。我创建了一个示例应用程序,用户将在其中输入用户名和密码并将其发送到 Web 服务。

用户名和密码字段应该在“VIEW”中,“将其发送到 Web 服务”功能应该在“CONTROLLER”中。

在我的程序中,发送到网络服务的部分也在“视图”中,所以有人可以帮我编辑我的代码,这样我就可以在“控制器”中拥有“发送到网络服务”功能

这是代码

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();

                   }
           }
           ]



           }




           });
4

1 回答 1

1

首先,设置id按钮的属性。

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

在控制器类中,

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');
                 }
             });        
    }
});

注意: insert.php将是服务器端代码(只是一个例子),它将与您的数据库建立连接并在其中输入值。

于 2012-05-05T16:54:06.737 回答