-->

Passing values back to the 'View' from 

2019-09-15 18:04发布

问题:

In my VIew, i have added a button and when i click on it, the Controller class gets called. The value's in the View (textfield values) , will be sent over the URL from the web service. Then the server will return a JSON string shown below;

{"value":"SUCCESS"}

Now, i need to sent this JSON string back to the view, and the view will alert the user depending on the JSON response. If the JSON returned SUCCESS, then a Success alert will get displayed and if the JSON returned FAIL, then a Fail alert will be displayed.

1.) In my code, i can only display the JSON response from the VIEW, but how can i send it to the COntroller ? (var text = response.responseText; displays the JSON response)

2.) How could i, separate the string from the view, and only get the SUCCESS or FAIL string from the JSON response ?

Button Implementation, From the View Class

xtype:'button',
                   id:'when_button_click',
                   text:'Send',
                   ui:'confirm',

The COntroller CLass

Ext.define('myapp.controller.testcont', {

                  extend: "Ext.app.Controller",
                  config: {
                  refs: {
                  newNoteBtn: "#when_button_click"
                  },
                  control: {
                  newNoteBtn: {
                  tap: "onNewNote"
                  }
                  }
                  },
                  onNewNote: function () {
 var values = Ext.getCmp('form').getValues();
                 console.log("inside onNewNote function");

       Ext.Ajax.request({
                        url: 'http://call.com/the_webservice',
                        params : values,

                        failure: function (response) {
                        var text = response.responseText;
                         console.log("fail");

                        },                              success: function (response) {
                        var text = response.responseText;
                         console.log("success");

                        }

                        });



                  }

                  // init and launch functions omitted.
                  });

回答1:

1.) In my code, i can only display the JSON response from the VIEW, but how can i send it to the COntroller ?

Why you want to pass the values back to the view from the controller?

I don't see any valid reason to do so.

You can show the Ext.Msg.alert in the controller itself on success and failure of Web Service like this,

.....
.....
url: 'http://call.com/the_webservice',
params : values,

failure: function (response) {
   var text = response.responseText;
   Ext.Msg.alert('Error','Error while executing Web Service');
},

success: function (response) {
   var text = response.responseText;
   Ext.Msg.alert('Success','Web Service code successfully executed');
},
.....
.....

2) How could i, separate the string from the view, and only get the SUCCESS or FAIL string from the JSON response ?

EDIT:

Do something like this ..

var result = Ext.decode(response.responseText);

// result.value = SUCCESS or FAIL
Ext.Msg.alert('Message',result.value);