2012-11-09 56 views
4

好吧,努力獲得放大解碼器。奇怪的問題。如果我有一個beforeSend附加到我的請求,解碼器不會觸發。刪除beforeSend和解碼器觸發。amplifyjs解碼器和阿賈克斯beforeSend

以下是兩個例子。

  1. 沒有beforeSend。

http://jsfiddle.net/sujesharukil/Td2P4/12/

  1. 隨着beforeSend

http://jsfiddle.net/sujesharukil/Td2P4/14/

有人能告訴我發生了什麼?如果我有一個beforeSend,爲什麼解碼器不工作?我假設解碼器應該在接收到請求後觸發,所以beforeSend不應該對它有任何影響!

注:計算器要我在這裏發佈的代碼,不只是撥弄

//please check the fiddles

amplify.request({ 
    resourceId: "testRequest", 
    data: { 
     json: JSON.stringify({ 
      text: 'hello world' 
     }) 
    }, 
    success: function(data, status) { 
     console.log(data, status); 
     $('.messages').append('<div> text retrieved: ' + data.text + '</div>'); 
    }, 
    error: function(status, xhr) { 
     console.log(xhr); 
    } 
});​ 

幫助?

-Suj

回答

9

好吧,算出來。

在amplifyjs本節吸引了我的眼球

beforeSend : function(_xhr, _ajaxSettings) { 

       xhr = _xhr; 
       ajaxSettings = _ajaxSettings; 
       var ret = defnSettings.beforeSend ? 
        defnSettings.beforeSend.call(this, ampXHR, ajaxSettings) : true; 
       return ret && amplify.publish("request.before.ajax", 
        defnSettings, settings, ajaxSettings, ampXHR); 
      } 
     }); 

需要注意的是,它會調用beforeSend如果指定了它,否則設置var ret如果設置爲true,設置爲true

,它會發布"request.before.ajax"

下載到文件中,放大聽此所以,如果你有一個beforeSend,如果它沒有返回true,該消息從未出版和解碼器從不打消息

amplify.subscribe("request.before.ajax", function(resource, settings, ajaxSettings, ampXHR) { 
var _success = ampXHR.success, 
    _error = ampXHR.error, 
    decoder = $.isFunction(resource.decoder) 
     ? resource.decoder 
     : resource.decoder in amplify.request.decoders 
      ? amplify.request.decoders[ resource.decoder ] 
      : amplify.request.decoders._default; 

if (!decoder) { 
    return; 
} 

function success(data, status) { 
    _success(data, status); 
} 
function error(data, status) { 
    _error(data, status); 
} 
ampXHR.success = function(data, status) { 
    decoder(data, status, ampXHR, success, error); 
}; 
ampXHR.error = function(data, status) { 
    decoder(data, status, ampXHR, success, error); 
}; 

});

解決方案?從beforeSend功能

還真

amplify.request.define("testRequest", "ajax", { 

url: "/echo/json/", 
dataType: 'json', 
type: 'POST', 
decoder: function(data, status, xhr, success, error) { 
    console.log('decoder fired'); 
    $('.messages').append('<div>decoder fired </div>'); 
    success(data); 
}, 
beforeSend: function(xhr){ 
//not doing anything here, just logging; 
    console.log('before send fired'); 
    $('.messages').append('<div>before send fired </div>'); 
    return true; //this is the key 
} 

});

的作品就像一個魅力!希望這可以幫助別人試圖解決這個問題!

+0

這確實幫了我,謝謝。在我的情況下,我試圖定義一個自定義的緩存,遇到了同樣的問題。找不到任何文檔覆蓋此。讚賞您的張貼您的研究! – Soulriser

0

剛剛從示例頁面複製了這個。希望能幫助到你。

amplify.request.decoders.appEnvelope = 
    function (data, status, xhr, success, error) { 
     if (data.status === "success") { 
      success(data.data); 
     } else if (data.status === "fail" || data.status === "error") { 
      error(data.message, data.status); 
     } else { 
      error(data.message , "fatal"); 
     } 
    }; 

amplify.request.define("decoderExample", "ajax", { 
    url: "/myAjaxUrl", 
    type: "POST", 
    decoder: "appEnvelope" // <--- a function name(string) and not a function. 
}); 

amplify.request({ 
    resourceId: "decoderExample", 
    success: function(data) { 
     data.foo; // bar 
    }, 
    error: function(message, level) { 
     alert("always handle errors with alerts."); 
    } 
}); 
+0

謝謝Stefan的迴應。我的東西也來自他們的文檔,正如我所說的,除非請求中有beforeSend,否則它的效果很好。那就是它不會觸發解碼器的地方。所以不,你的迴應沒有幫助。:( –