2012-11-09 100 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,否则它的效果很好。那就是它不会触发解码器的地方。所以不,你的回应没有帮助。:( –