2012-07-10 43 views
2

我寫我的代碼如下:傳遞迴調的正確方法是什麼?

var MyLib = (function (window, $, undefined) { 
    return { 
     URI: 'http://testpage/API/', 
     OnSuccess: function (data, status) { }, 
     OnError: function (request, status, error) { }, 
     MakeRequest: function (method, args) { 
      $.ajax({ 
       type: 'POST', 
       url: this.URI + '/' + method, 
       contentType: 'application/json; charset=utf-8', 
       data: args, 
       dataType: 'json', 
       success: this.OnSuccess, 
       error: this.OnError 
      }); 
     }, 
     GetSamples: function (data1, data2) { 
      var args = { 
       data1: data1, 
       data2: data2 
      }; 
      this.MakeRequest('GetTestData', JSON.stringify(args)); 
     } 
    }; 
} (this, jQuery)); 

所以,如果我想調用的AJAX調用,我會做:

function OnSuccess(data, status) { 
    // ... 
} 

function OnError(request, status, error) { 

} 

MyLib.OnSuccess = OnSuccess; 
MyLib.OnError = OnError; 
MyLib.GetSamples("data1", "data2"); 

我不想改變GetSamples簽名因此我選擇如上所述來實施它。任何關於這是否是可接受的方法(或如何改進這一點)的建議?

+0

對我來說很不錯 – 2012-07-10 06:54:05

回答

1

您也可以返回jQuery AJAX對象並在其上使用請求時調用.done()。

像:

var MyLib = (function (window, $, undefined) { 
    return { 
     URI: 'http://testpage/API/', 
     OnSuccess: function (data, status) { }, 
     OnError: function (request, status, error) { }, 
     MakeRequest: function (method, args) { 
      return $.ajax({ 
       type: 'POST', 
       url: this.URI + '/' + method, 
       contentType: 'application/json; charset=utf-8', 
       data: args, 
       dataType: 'json' 
      }); 
     }, 
     GetSamples: function (data1, data2) { 
      var args = { 
       data1: data1, 
       data2: data2 
      }; 
      return this.MakeRequest('GetTestData', JSON.stringify(args)); 
     } 
    }; 
} (this, jQuery)); 

然後:

function OnSuccess(data, status) { 
    // ... 
} 

function OnError(request, status, error) { 

} 

MyLib.GetSamples("data1", "data2").done(OnSuccess).fail(OnError); 

這些被稱爲jQuery的deferreds,看一看的API。海事組織這是一個非常乾淨的方式來處理異步調用。

3

不是很javascript的習慣用法。看起來更像是.NET代碼。在javascript中提供回調的標準方法是將它們作爲參數傳遞。但如果你不能修改GetSamples方法的簽名,那麼我想這種方法也適用於你的情況。我只是不會將其推廣到所有的API。僅在此特定情況下用作解決方法。

1

我會同意Darin Dimitrov,並建議只使用一個回調。成功和失敗不是兩個。例如:

MyLib.GetSamples("data1", "data2", function(err, response) { 

}); 

通過使用這種方法,你所處理的只是一個回調,你一定會認爲這要使用你的類開發商也不會忘記檢查錯誤(這是把這個想法首先是err屬性)。

1

有趣的話題。我看到很多JavaScript開發者做這樣的事情:

(function ($, window, document) { 

    "use strict"; 

    var App = (function() { 

    function App() { 
     this.url = 'http://testpage/API'; 
     this.debug(); 
    } 

    App.prototype = { 

     url: 'http://testpage/API', 

     success: function (data, status) {}, 

     error: function (req, status, err) {}, 

     request: function (command, options) { 
     $.get({ 
      type: 'POST', 
      url: this.url + '/' + command, 
      contentType: 'application/json; charset=utf-8', 
      data: options || {}, 
      success: this.success, 
      error: this.error 
     }); 
     }, 

     getSample: function (data1, data2) { 
     this.request('getTestData', JSON.stringify({ 
      data1: data1, data2: data2 
     })); 
     } 


    }; 

    return App; 

    })(); 


})(jQuery, window, document); 

我猜原型方法使用,這樣,如果你需要你的應用程序有多個實例在頁面中,你不必再 - 定義方法。

另外需要注意的是,在JavaScript中,主要的命名約定是camelCase。我的同事在Backbone.js中寫了一個模式列表,它也適用於JS設計模式。 http://ricostacruz.com/backbone-patterns/

相關問題