I want to do the following with my javascript codeblock.
- Handle all current and new device requests ie. detect,encrypt,decrypt,etc
- Return the result to the calling method
Questions
- How can I improve the existing code and get rid of the javascript strict warning:anonymous function does not always return a value.
- What is the right way of calling my method?
Any help is greatly appreciated
Thanks!
Herewith the code:
This is how I call the current method
//Contents of SmEditor.js
var response = Ext.decode(Prometheus.DeviceRequestHelper.detect(request_id));
//contents of Sm.js
Ext.ns('myApp')
myApp.DeviceRequestHelper = {
detect:function(request_id){
var task = function(){
Ext.Ajax.request({
url: 'device_requests.php',
params:{
action:'get_device', //in php
'request_id':request_id
},
timeout:30000, //30 seconds
success:function(response){//serverside response
var result = Ext.decode(response.responseText); //convert to js objects
if(result.success == true){//device was detected
cons.log('success,device was detected');
cons.log(result);
Ext.TaskMgr.stop(runTask);
return Ext.encode(result); //javascript strict warning
}else{
if(runTask.taskRunCount >= 10){
//retry limit exceeded
Ext.Msg.show({
title:'Server Failure',
msg:"Detection Failed,Unable to detect device",
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
Ext.MessageBox.getDialog().getEl().setStyle('z-index','80000');
Ext.TaskMgr.stop(runTask);
}
}
},
failure:function(response){
Ext.TaskMgr.stop(runTask);
Ext.Msg.show({
title:'Server Failure',
msg:"Failed, server communication error",
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
Ext.MessageBox.getDialog().getEl().setStyle('z-index','80000');
}
})
}
var runTask = {
run: task,
interval:2000,
repeat:10
};
Ext.TaskMgr.start(runTask);
}
}
To prevent this kind of warning, have the function
returna value in all cases, or no cases. At the moment you’re only returning a value in oneifcase; the other cases will not return anything. You can evenreturn undefinedto make the warning go away. However, what it is telling you is correct: that a function that sometimes has a return value and sometimes doesn’t is a bit weird and suggests you’re doing something wrong.What you seem to want to do is have the inner
returnin thesuccessmethod return a value from thedetect()method. This is absolutely not possible. The inner function can only return a value to the caller ofsuccess, which is Prototype itself. By the time this happens, thedetect()method has long since returned.What you have here is asynchronous code. The
detect()method can set up an AJAX request, but it must then return immediate to its caller, which will return control to the browser. At some later time, the HTTP request behind the AJAX call will complete, and then thesuccessfunction will fire. JavaScript cannot call asynchronous code synchronously, or vice versa.What you have to do is pass a callback function into your method, and then call it back on completion:
(I removed the extra
Ext.encode->Ext.decodepair, that just seems like a waste of time.)