2012-05-05 34 views
1

我創建了一個MVC SenchaTouch項目。 我已經創建了一個示例應用程序,用戶將輸入用戶名和密碼並將其發送到Web服務。向我的基於MVC的Sencha Touch項目添加'Controller'類

用戶名和密碼字段假設在'VIEW'中,'發送給Web服務'功能應該在'CONTROLLER'中。

在我的程序中,發送到Web服務部分也存在於'VIEW'中,所以有人可以幫我編輯我的代碼,以便我可以在'發送到Web服務'功能'控制器'

下面的代碼

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

首先,設置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將是服務器端代碼(只是一個e.g),這將使你的數據庫的連接,並在其中輸入值。

+0

1.)我必須擺脫'處理程序:函數(){這個。(contactform).submit(); }'contactView.js'的代碼行? 2.)MyApp.controller.Main是Controller類的名字嗎? (我的控制器類被稱爲'myController.js')? – user1315906

+0

1)由於您需要從控制器文件中調用Web服務代碼,因此您可以安全地從視圖中刪除該處理函數.... 2)'Main.js'是我的控制器文件的名稱。只需將它更改爲''myController.js''即可。 僅供參考,'MyApp'是我爲我的應用程序提供的示例名稱 –

+0

我在alert /'Web服務代碼前往此處添加了一個alert語句alert('come here');'線沒有得到執行。任何線索? – user1315906

相關問題