Do we ask the XHR object to be asynchronous?(关于DWR同步异步再思)
DWR是AJAX的服务器端框架,它封装了服务器端的调用,我们可以在JAVASCRIPT中直接调用服务器端的代码,完成想要的功能。通过简单DWR使用我们可以完成复杂的AJAX功能!这使得编程如此简单!
由于基于AJAX的DWR也同样继承了异步独特功能,
在享受异步便捷的时候似乎也给同步带来烦恼!
看一段dwr回调函数代码:
function addTache(tacheId,tacheName){ServicesManager.addTache(<%=servId%>,tacheId,tacheName,callBackForaddTache);}function callBackForaddTache(data){alert(data);return data;}
dao.代码:该代码获取插入信息的主键Id:
public int addTache(String servId,String tacheId,String tacheName){Connection cnn = DAOFactory.getConnection();PreparedStatement ps = null;ResultSet rs = null;int i =0;try { ps = cnn.prepareStatement(SQL.insert_Tache); ps.setString(1,servId); ps.setString(2,tacheId); ps.setString(3,tacheName); i = ps.executeUpdate(); //取当前case_IDint rows = ps.getUpdateCount();boolean moreResults = true;while (moreResults || rows != -1) {try {rs = ps.getResultSet();}catch (IllegalArgumentException e) {e.printStackTrace();}if (rs != null) {rs.next();i = rs.getInt( 1 ) ;}moreResults = ps.getMoreResults();rows = ps.getUpdateCount();}} catch (SQLException e) {// TODO 自动生成 catch 块e.printStackTrace();}finally{DAOFactory.close(ps);}return i;}
以上配置我们可以通过callBackForaddTache(data)获取插入信息的主键返回值!
但是如何赋值给js变量,进行删除或修改操作呢?!
把回调函数放在一起,修改js代码:
function addTache(tacheId,tacheName){var back="";ServicesManager.addTache(<%=servId%>,tacheId,tacheName,function(data){alert(data);back=data;alert(back);});alert(back)return back;}
通过上面测试,可以发现alert(data);alert(back);都有值,但是下面的alert(back)却没有获取任何信息!
呵呵,理解了异步了吗?!
由于异步的特殊机制,导致异步获取的值但是在同步下却导致没有值可取!
在享受异步便捷的时候似乎也给同步带来烦恼!
这个时候,我们需要“返璞归真”!不要异步!!!
可以说,AJAX还是很活跃的,提供了相关的功能!
AJAX仍然也支持同步的调用。在纯粹的XMLHttpRequest中可以设置调用是否是异步的。XMLHttpRequest的open函数是有一个是否同步参数,如下:
XMLHttpRequest.open(String method, String URL, boolean asynchronous)其中的asynchronous就是是否同步的参数了。
呵呵,这么好的功能,DWR也是支持的!
在DWR的engine.js文件,有setAsynchronous方法,就是设置调用是否是同步的,还是异步的。(可能版本不同,方法会有些区别:如,DWREngine.setAsync(false); )
看看描述的功能吧!
/** * Do we ask the XHR object to be asynchronous? (Default: true) * Warning: This option will be ignored if you are using iframes and is often * generally a bad idea to change it to false because it can make your browser * slow to respond and prone to hangs. If you do need this option then consider * setting a timeout too. * @param asynchronous true or false */DWREngine.setAsynchronous = function(asynchronous) {DWREngine._asynchronous = asynchronous;};
呵呵,那么我们修改asynchronous,Default 的value:flase!先同步会!
不会断掉DWR的饭碗呢?\(^o^)/~,放心,在需要的地方使用后,再赋值:true!再异步!
function addTache(tacheId,tacheName){ DWREngine.setAsynchronous(false); var back="";ServicesManager.addTache(<%=servId%>,tacheId,tacheName,function(data){//alert(data);back=data;//alert(back);});DWREngine.setAsynchronous(true);return back;}
ok,万事大吉,我们调用代码:
function configTache(){varflow ="0";deleteTache(<%=servId%>);var taches = document.all["allfee"];for (var i=0; i<taches.options.length; i++){ var text = taches.options.text;var index = text.indexOf(")");var info2 = text.substring(index+1);var dwr = addTache(taches.options.value,info2)configSequence(i,dwr);flow += "," + taches.options.value;}ServicesManager.updateTache(<%=servId%>,flow);window.cfmFee.btn_ok.disabled =false;window.close();//window.location.href="tacheMgr.jsp";}
页:
[1]