本文介绍了向我的基于MVC的Sencha Touch项目添加一个“Controller”类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

假设用户名和密码字段位于VIEW,发送到Web服务功能应该在CONTROLLER中。

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'.

在我的程序中,发送到Web服务部分也在'VIEW',所以有人可以帮助我编辑我的代码, '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'

以下是代码

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

                   }
           }
           ]



           }




           });


推荐答案

首先,设置 id

Firstly, set the id property for your button.

 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 将是服务器端代码(只是一个eg),它将与您的数据库建立连接并在其中输入值。

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.

这篇关于向我的基于MVC的Sencha Touch项目添加一个“Controller”类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:53