I have a webserverAPI js, and ui.js my ui.js have require this webserverAPI js. The webserver javascript can get some data and return. the invoke webserver have to command this function.
ui.js
var result = require('webserverAPI').getPieChar(pickerSelLabel.item);webserverAPI.js
function getSomeData(item) { Ti.include('suds.js'); var suds = new SudsClient(); var obj = {value:''}; suds.invoke('xxx', args, function(value){ // this value can get data from webserver obj.value = value; Ti.API.info(' ===AAA=== value : ' + obj.value ); } ); Ti.API.info('===BBB=== value:' + obj.value ); return obj; } exports.getSomeData = getSomeData;But I got result is
trace: ===BBB=== value: // is empty ===AAA=== value: balba // have valuehow can I do return obj to ui.js resulst varialbe? this is some time problem.
thank you very much
5 Answers
Accepted Answer
Try this:
require('webserverAPI').getPieChar(pickerSelLabel.item, function(e){ alert(e); });
function getPieChar(item, callback) { Ti.include('suds.js'); var suds = new SudsClient(); var obj = {value:''}; suds.invoke('xxx', args, function(value){ // this value can get data from webserver obj.value = value; Ti.API.info(' ===AAA=== value : ' + obj.value ); callback(obj); } ); Ti.API.info('===BBB=== value:' + obj.value ); } exports.getPieChar = getPieChar;
What you need is a callback. You could add this as a second parameter to your function, here is a simple and quick example
/** * Get some data * @param {Object} params XHR parameters * @param {Function} callback Callback function */ function getSomeData(params, callback){ // do something here: var xhr = Ti.Network.createHTTPClient(); xhr.onload = function(){ var obj = this.responseText; callback(obj) } xhr.open(params.method, params.url); xhr.send(); } // Call getSomeData() getSomeData({ url: 'http://google.com/', method: 'GET' }, function(response){ // do something with the response alert(response); });
sorry, I'm not sure how to fix. is it? thank you very much
function getSomeData(item) { Ti.include('suds.js'); var suds = new SudsClient(); var obj = {value:''}; function getSomeData(params, callback){ suds.invoke('xxx', args, function(value){ // this value can get data from webserver obj.value = value; callback(value); } ) obj.value = value; Ti.API.info('===BBB=== value:' + obj.value ); return obj; } exports.getSomeData = getSomeData; }
my suds.js is from * License: http://www.apache.org/licenses/LICENSE-2.0.html * Source: http://github.com/kwhinnery/Suds
invoke function is below sud.js
this.invoke = function(_soapAction,_body,_callback,_error) { var body = _body; //Allow straight string input for XML body - if not, build from object if (typeof body !== 'string') { body = '<'+_soapAction+' xmlns="'+config.targetNamespace+'">'; body += convertToXml(_body); body += '</'+_soapAction+'>'; } var ebegin = config.envelopeBegin; config.envelopeBegin = ebegin.replace('PLACEHOLDER', config.targetNamespace); if (config.targetNamespace.lastIndexOf('/') != config.targetNamespace.length - 1) { soapAction = config.targetNamespace+'/'+_soapAction; } else { soapAction = config.targetNamespace+_soapAction; } //POST XML document to service endpoint var xhr = getXHR(); xhr.onload = function() { var xmlDoc=xmlDomFromString(this.responseText) var propertyList = xmlDoc.documentElement.getElementsByTagName(_soapAction+"Result"); _callback.call(this, propertyList.item(0).text); }; xhr.onerror = function(msg){ _error.call(this,msg); } xhr.setTimeout(config.timeout); var sendXML = config.envelopeBegin+body+config.envelopeEnd; xhr.open('POST',config.endpoint); xhr.setRequestHeader('Content-Type', 'text/xml'); xhr.setRequestHeader('Soapaction', soapAction); if (config.authorization != undefined) { xhr.setRequestHeader('Authorization', 'Basic ' + config.authorization); } xhr.send(sendXML); };
Instead of asking for someone to fix your code, you should try to understand the answers given here. In my first attempt, I suggested to use a callback, and in my second attempt I tried to explain to you how to use it in your code.
Go to google and look for "how to use callbacks in javascript" to know what they are, what they are used for, and how to properly use them. In my first answer I gave you all you needed to know to continue.
If you are using a third party library, like suds, you should ask the developer about it if you have questions.
Your Answer
Think you can help? Login to answer this question!