Javascript:如何在处理程序中保留对请求发起者的引用? [英] Javascript: How do I retain a reference to a request initiator in a handler?

查看:248
本文介绍了Javascript:如何在处理程序中保留对请求发起者的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常不是一个Javascript人物,但我一直在潜水,阅读 Douglas Crockford的书a>,并撰写一些微不足道的有用的信息作为Chrome扩展和 Node.js (请注意,这个问题不是关于的任何一方)。



现在,我试图找出如何保留对发起AJAX请求的对象的引用,即:一旦我设置了onload事件处理程序这是来自Chrome扩展程序,所以我使用的是基础的XMLHttpRequest对象),有没有什么方法可以回到 MyObject 在下面的例子中: p>

  MyObject.prototype = {
PerformAction:function(){
this.Request = new XMLHttpRequest();
this.Request.open(GET,this.ConstructUrl(),true);
//从ActionResultHandler中,'this'将永远是XMLHttpRequest
this.Request.onload = this.ActionResultHandler,
this.Request.send(null);
}
}

正是这样才能分配这个成为请求对象本身,如果我只是介绍一个包装器:

  .Request.onload = function(){ActionResultHandler()};好吧,那只是不会做任何,因为这个ActionResultHandler现在超出范围。我在这里询问的原因是因为我只发现调用者操纵的微不足道的例子(例如,操纵函数内部的这个),但是由于OO-如果Javascript和AJAX是无处不在的,这必须是一个已知的,简单的问题,但是我的Google-fu在这里失败了。在C#中,事件是在连接到他们的环境中调用的,而不是触发事件的对象,所以这不是每天都出现的。也许有一个更好的JS模式完全避免了这个问题?

解决方案

我不清楚你想要保存哪个变量参考。以下是在您的onload处理程序中保留对 MyObject 的引用:

  MyObject.prototype = {
PerformAction:function(){
var MyObjectRef = MyObject,
ActionResultHandler = this.ActionResultHandler;

this.Request = new XMLHttpRequest();
this.Request.open(GET,this.ConstructUrl(),true);
//从ActionResultHandler中,'this'将永远是XMLHttpRequest
this.Request.onload = function(){
ActionResultHandler.apply(MyObjectRef,arguments);
};
this.Request.send(null);
}
}

已编辑



好的,我再次重读你的问题,似乎你想在MyObject的上下文中执行ActionResultHandler,所以我调整了我的代码来做到这一点。


I'm not a Javascript person normally, but I've been diving in, reading Douglas Crockford's book, and writing some trivial, useful tidbits as Chrome extensions and Node.js (note that this question isn't about either of them).

Right now, I'm trying to figure out how to retain a reference to an object that's initiating an AJAX request, that is: once I set the onload event handler (this is from inside a Chrome extension, so I'm using the base XMLHttpRequest object), is there any way that I can refer back to MyObject in the following example:

MyObject.prototype = {
    PerformAction: function() {
        this.Request = new XMLHttpRequest();
        this.Request.open("GET", this.ConstructUrl(), true);
        // From within ActionResultHandler, 'this' will always be the XMLHttpRequest
        this.Request.onload = this.ActionResultHandler,
        this.Request.send(null);
    }
}

Doing this exactly is going to assign this to be the request object itself, and if I simply introduce a wrapper:

this.Request.onload = function() { ActionResultHandler() };

well, that just isn't going to do anything, because the ActionResultHandler is now out of scope. The reason I'm asking here is because I've only found trivial cases of caller manipulation (e.g. manipulating what this refers to from inside a function), but given that OO-ified Javascript and AJAX are literally everywhere, this has to have to be a known, simple issue, but my Google-fu is failing me here. In C#, events are invoked in the context of whoever attaches to them, not the object firing the event, so this doesn't come up on a daily basis. Perhaps there's a much better JS pattern that avoids this issue entirely?

解决方案

It's not really clear to me which variable you want to hold a reference to. Here's how you would retain a reference to MyObject in your onload handler:

MyObject.prototype = {
    PerformAction: function() {
        var MyObjectRef = MyObject,
            ActionResultHandler = this.ActionResultHandler;

        this.Request = new XMLHttpRequest();
        this.Request.open("GET", this.ConstructUrl(), true);
        // From within ActionResultHandler, 'this' will always be the XMLHttpRequest
        this.Request.onload = function () {
                ActionResultHandler.apply(MyObjectRef, arguments);
            };
        this.Request.send(null);
    }
}

Edited

Ok, I reread your question again and it seems that you want to execute ActionResultHandler in the context of MyObject, so I tweaked my code to do this.

这篇关于Javascript:如何在处理程序中保留对请求发起者的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆