Google 关闭:集中式 AJAX“解码器”?
首先,我必须说我对 Google Closure 很陌生,但我正在学习:)
好的,所以我正在制作一个非常大的网络应用程序,我认为管理起来会很好所有 AJAX 请求都在一个 XhrManager 中。那里没问题。
但是,是否有可能有某种默认回调首先检查错误,在必要时显示错误,然后在错误通过时启动“真正的”回调?我说的是像 amplify.js 中的解码器这样的功能。这是他们的解释:
<块引用>解码器允许您在调用成功或错误回调之前解析 ajax 响应。这允许您返回标有状态的数据并做出相应的反应。这还允许您在将数据传递给回调之前以任何您想要的方式操作数据。
我知道这听起来很复杂(确实如此),所以我不擅长解释这一事实也有很大帮助,但是是的。
我现在想到的解决方案是创建一个存储所有“真实回调”的对象,其中“错误检查回调”将在完成检查后执行正确的回调,但我觉得这有点黑客攻击我认为必须有更好的方法来实现这一点。
First of all, I must say that I'm very new to Google Closure, but I'm learning :)
Okay, so I'm making a web app that's going to be pretty big, and I thought it would be good to manage all the AJAX requests in one XhrManager. No problem there.
But, is it possible to have some kind of default callback that would check for errors first, display them if necessary and then when it passes, launch the "real" callback? I'm talking about a feature like the decoders in amplify.js. Here's their explanation:
Decoders allow you to parse an ajax response before calling the success or error callback. This allows you to return data marked with a status and react accordingly. This also allows you to manipulate the data any way you want before passing the data along to the callback.
I know it sounds complicated (and it is, really), so the fact that I'm not that good at explaining helps a good deal too, but yeah.
The solution I have in my head right now is creating an object that stores all the 'real callbacks', of which the 'error-checking callback' would execute the correct one after it finished checking, but I feel that's a bit hack-ish and I think there has to be a better way for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您总是必须解码/验证您的 AJAX 数据(您现在从不信任从服务器返回的数据,是吗?),因此您总是会拥有不同的解码器/不同类型 AJAX 负载的验证器。因此,您可能应该将解码器/验证器例程作为 AJAX 回调本身进行传递 - 对于所有数据类型通用的验证,请在回调内调用通用函数。
这样做的另一个好处是能够将未损坏的 JSON 对象“转换”为“损坏的”JSON 对象,这样您就不必在代码中使用带引号的属性访问。
例如,假设您的 AJAX 负载由以下 JSON 对象组成:
{ "hello":"world" }
如果您想在代码中引用
hello
属性并且仍然通过编译器的高级模式,您需要执行obj["hello"]
。但是,如果您传入解码器作为回调,并且执行第一行:var demod = { hello:response["hello"] };
然后在返回 < 之前进行错误检查等code>解码作为AJAX响应。在您的代码中,您只需执行
obj.hello
即可,所有内容都将通过高级模式得到很好的优化和破坏。Since you always have to decode/verify your AJAX data (you never trust data returned from a server now do you?), you're always going to have different decoders/verifiers for different types of AJAX payloads. Thus you probably should be passing the decoder/verifier routine as the AJAX callback itself -- for verifications common to all data types, call a common function inside the callback.
An added benefit of this will be the ability to "translate" unmangled JSON objects into "mangled" JSON objects so that you don't have to do use quoted property access in your code.
For example, assume that your AJAX payload consists of the following JSON object:
{ "hello":"world" }
If you want to refer to the
hello
property in your code and still pass the Compiler's Advanced Mode, you'll need to doobj["hello"]
. However, if you pass in your decoder as the callback, and the first line you do:var decoded = { hello:response["hello"] };
then do your error checking etc. before returning
decoded
as the AJAX response. In your code, you can simply doobj.hello
and everything will be nicely optimized and mangled by Advanced Mode.