2014-02-19 32 views
5

這是一個關於使JavaScript調用生成標準「連接到QuickBooks」按鈕(用於通過Intuit的v3 REST API建立與QuickBooks Harmony的連接)的最佳實踐的問題。調用intuit.ipp.anywhere.setup()的最佳實踐?

如果我按照Intuit的例子,我想:

  1. 參考https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js在腳本標籤。
  2. 放置< IPP:connectToIntuit > </IPP:connectToIntuit >標記集,我想「連接到QuickBooks的」按鈕,顯示
  3. 跨我的手指,希望intuit.ipp.anywhere.js 沒有進行重定向到停工的消息,再次 仍然存在
  4. 讓我呼籲intuit.ipp.anywhere.setup()
  5. 請參見 「連接到QuickBooks的」 按鈕

...它的工作原理(關於「作品」多值),但感覺很脆弱:

  1. 如果intuit.ipp.anywhere.js被重定向到一個停機消息(讀:不是的JavaScript)或不可用,我會得到腳本錯誤。
  2. 如果我得到一個腳本錯誤(或者Intuit的腳本副本出現其他問題),則沒有任何反饋給用戶,只是「連接到QuickBooks」按鈕的空白處。

爲了使這一切更有彈性,我將intuit.ipp.anywhere.js的引用和intuit.ipp.anywhere.setup()調用合併爲一個JQuery .ajax()調用:

$.ajax({ 
    url: 'https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js', 
    type: 'GET', 
    dataType: 'script', 
    timeout: 4000, 
    success: function(response) { 
     if (typeof intuit !== 'undefined') { 
     intuit.ipp.anywhere.setup({ 
     menuProxy: 'MYMENUPROXYURL.aspx', 
     grantUrl: 'MYGRANTURL.aspx' 
     }); 
     } 
    }, 
    error: function(x, t, m) { 
     // show some friendly error message about Intuit downtime 
    }   
    }); 

...也可以工作(即「作品」的幾個值):

  1. 我的設置()調用被包裝的成功處理程序(和其他檢查裏面intuit Object的存在),所以如果事情出錯,我不應該得到一個腳本錯誤。
  2. 如果Intuit腳本的GET超時(4000毫秒後)或返回的內容不是腳本,我會向用戶顯示一條友好的錯誤消息。

其他人採取了不同的方法? Intuit是否回到線上?

+0

嘿,這看起來像一個很好的開始 - 這是否使瀏覽器停止使用本地緩存,但? –

+0

@TroyAnderson我們使用'.getScript'函數默認將緩存設置爲'false'。在Machete的文章中,他可以爲'.ajax'參數添加一個顯式的'cache:false'。 – dthagard

+0

@TroyAnderson在'.ajax'參數中不需要顯式的'cache:false',因爲'dataType:script'會默認緩存值爲false([reference](http://api.jquery.com/jquery阿賈克斯/)) –

回答

2

這與我們處理它的方式類似。我們用jQuery.getScript調用了它,但顯然.fail處理程序不支持跨域腳本標記。我們的解決方案如下:

<script type="text/javascript> 
    var timeoutID; 
    timeoutID = window.setTimeout(function() { 
     $("#ippConnectToIntuit").replaceWith('<p class="error-message">There was a problem communicating with QuickBooks. The service may be down or in heavy use. Try again later.</p>'); 
     }, 5000); 
    $.getScript("https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js") 
     .done(function() { 
      window.clearTimeout(timeoutID); 
      intuit.ipp.anywhere.setup({ 
       menuProxy: '/path/to/our/menu/proxy', 
       grantUrl: '/path/to/our/grant/url' 
      }); 
     }); 
</script> 
<div id="ippConnectToIntuit"><ipp:connecttointuit></ipp:connecttointuit></div>