[SOLVED] How to return a Promise after fireWidgetEvent

Answered
0
0

Hi,

Below is my code to send update to server, but unfortunately, there is an error saying cannot

“Uncaught TypeError: Cannot read property ‘done’ of “
me.fireWidgetEvent(“ValueUpdate”, {values: JSON.stringify(values)})
.done (function(data){
d.resolve(data);
})
.fail(function(xhr,status,error){
d.reject();
});
return d.promise();

Hope you can show me a sample code with this scenario.

 

Thanks.

  • You must to post comments
Best Answer
0
0

Hi Glenn,

I forgot one, you can also use this:

App.MainPage.TestFuncAsync().then(r => alert(r));

It might cause an issue at design time because the Wisej Designer uses IE.

Best,

Levie

  • You must to post comments
Great Answer
0
0

Hi Glenn,

You can use a WebMethod to achieve the promise functionality. Using fireWidgetEvent doesn’t return any data from the server.

I’m attaching a sample demonstrating three different ways you can communicate with the server and get the response back in JavaScript.

Check out the method marked with WebMethod in Page1.cs and the Widget’s initScript.

JS

this.init = function(options) { 

if (!wisej.web.DesignMode) { 

//Callback 
//App.MainPage.TestFunc(function(result) { alert(result); }); 

//Add async to function declaration. 
//var result = await App.MainPage.TestFuncAsync(); 
//alert(result); 

//Promise 
var promise1 = new Promise(function(resolve, reject) { 
App.MainPage.TestFunc(function(result) { resolve(result); }); 
}); 

promise1.then(function(result) { alert(result); }); 

} 
}

 

C#

[WebMethod]
 public string TestFunc()
 {
 // Do some logic
 return "done";
 }

 

Let me know if you have any questions about it!

Best regards,

Levie

Attachment
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.