Javascript 重用对“的序列化引用”;这"-关键词
第一件事:我不确定我要提供的信息是否足够,如果需要的话,我很乐意添加额外的信息 我正在将一个复杂的结构序列化为JSON格式,字段[I][0]是对对象的“this”引用 Firebug在JSON.Stringify(myObj)上的输出 这一切都很好,只要我保持它的一切。但现在我需要序列化并将其发送到后端,以获取引用+计算信息 现在我如何映射回我以前的引用?如何将此引用绑定回对象? 这个$$hash东西看起来是内部的和专有的,所以我甚至没有试过像Object[$$hash]=ref之类的东西 这一总体思路可能看起来很糟糕,但结果是异步返回的,我需要一个标识符将新信息绑定回原始对象。很明显,我可以为它编一个自己的标识符,但我想知道是否有一种方法可以这样解决它 编辑 对象是这样创建的(同样) 该对象具有如下方法Javascript 重用对“的序列化引用”;这"-关键词,javascript,oop,serialization,reference,this,Javascript,Oop,Serialization,Reference,This,第一件事:我不确定我要提供的信息是否足够,如果需要的话,我很乐意添加额外的信息 我正在将一个复杂的结构序列化为JSON格式,字段[I][0]是对对象的“this”引用 Firebug在JSON.Stringify(myObj)上的输出 这一切都很好,只要我保持它的一切。但现在我需要序列化并将其发送到后端,以获取引用+计算信息 现在我如何映射回我以前的引用?如何将此引用绑定回对象? 这个$$hash东西看起来是内部的和专有的,所以我甚至没有试过像Object[$$hash]=ref之类的东西 这
function GetRef(){
return this;
}
我用它在代码中保留一个ID/Ref
谢谢大家! 更新 如果您想更新一系列实例并发出许多Ajax请求,那么需要了解Ajax长轮询和排队技术。您将无法保留引用,但不管您使用什么Ajax技术,请使用以下技巧来保留引用 在顶部添加长轮询,您就可以开始了 这个想法是: 假设服务器将以JSON格式响应。如果您需要参考原始参考资料,这是我的两分钱: 在服务器答复时更新确切的引用。假设您有10个
某物的实例存储在一个数组中。在成功响应时,您可以使用Something
类中的方法以您想要的任何方式更新特定实例
/**
* The array with something instances.
* @type {Array.<Something>}
*/
var instances = [];
/**
* The Ajax success function.
* @param {Event} event The event object.
*/
function ajaxSuccess(event) {
var response = event.target.getResponseText();
var actualResponse = JSON.parse(response);
for (var i = 0, len = actualResponse.length; i++) {
instances[i].setWhatever(actualResponse[i].whatever);
};
};
让我们假设在后端,您有一个模仿客户端JavaScript模型的模型类。假设要更新显示文本的模型内的引用。我在后端使用Scala,但查看字段/属性并忽略语法
case class Article (
title: String,// these are my DB fields for an Article.
punchline: String,
content: String,
author: String
);
// now assume the client is making a request and the server returns the JSON
// for an article. So the reply would be something like:
{"title": "Sample title", "punchline": "whatever", "content": "bla bla bla boring", "author": "Charlie Sheen"};
// when you do
var response = JSON.parse(event.target.getResponseText());
// response will become a JavaScript object with the exact same properties.
// again, my backend choice is irrelevant.
// Now assume I am inside the success function, which gets called in the same scope
// as the original object, so it refers TO THE SAME THING.
// the trick is to maintain the reference with var that = this.
// otherwise the onSuccess function will be called in global scope.
// now because it's pointing to the same object.
// I can update whatever I want.
this.title = response.title;
this.punchline = response.punchline;
this.content = response.content;
this.author = response.author;
// or I can put it all in a single variable.
this.data = response;
您需要记住的是,范围需要保留。这就是诀窍。
当我做var=this时代码>我将引用复制到模型实例。通过高阶而非当前范围来记住引用
然后我告诉XMLHttpRequest
对象在完成后调用that.ajaxSuccess
。因为我使用了that
,所以将在当前对象的范围内调用ajaxSuccess
函数。因此在ajaxSuccess
函数中,this
将指向原始的this
,即相同的实例
JavaScript会在我编写var that=this时为我记住它代码>更新
如果您想更新一系列实例并发出许多Ajax请求,那么需要了解Ajax长轮询和排队技术。您将无法保留引用,但不管您使用什么Ajax技术,请使用以下技巧来保留引用
在顶部添加长轮询,您就可以开始了
这个想法是:
假设服务器将以JSON格式响应。如果您需要参考原始参考资料,这是我的两分钱:
在服务器答复时更新确切的引用。假设您有10个某物的实例存储在一个数组中。在成功响应时,您可以使用Something
类中的方法以您想要的任何方式更新特定实例
/**
* The array with something instances.
* @type {Array.<Something>}
*/
var instances = [];
/**
* The Ajax success function.
* @param {Event} event The event object.
*/
function ajaxSuccess(event) {
var response = event.target.getResponseText();
var actualResponse = JSON.parse(response);
for (var i = 0, len = actualResponse.length; i++) {
instances[i].setWhatever(actualResponse[i].whatever);
};
};
让我们假设在后端,您有一个模仿客户端JavaScript模型的模型类。假设要更新显示文本的模型内的引用。我在后端使用Scala,但查看字段/属性并忽略语法
case class Article (
title: String,// these are my DB fields for an Article.
punchline: String,
content: String,
author: String
);
// now assume the client is making a request and the server returns the JSON
// for an article. So the reply would be something like:
{"title": "Sample title", "punchline": "whatever", "content": "bla bla bla boring", "author": "Charlie Sheen"};
// when you do
var response = JSON.parse(event.target.getResponseText());
// response will become a JavaScript object with the exact same properties.
// again, my backend choice is irrelevant.
// Now assume I am inside the success function, which gets called in the same scope
// as the original object, so it refers TO THE SAME THING.
// the trick is to maintain the reference with var that = this.
// otherwise the onSuccess function will be called in global scope.
// now because it's pointing to the same object.
// I can update whatever I want.
this.title = response.title;
this.punchline = response.punchline;
this.content = response.content;
this.author = response.author;
// or I can put it all in a single variable.
this.data = response;
您需要记住的是,范围需要保留。这就是诀窍。
当我做var=this时代码>我将引用复制到模型实例。通过高阶而非当前范围来记住引用
然后我告诉XMLHttpRequest
对象在完成后调用that.ajaxSuccess
。因为我使用了that
,所以将在当前对象的范围内调用ajaxSuccess
函数。因此在ajaxSuccess
函数中,this
将指向原始的this
,即相同的实例
JavaScript会在我编写var that=this时为我记住它代码>你能给我们看一下对象是如何创建的吗?给我们看一下序列化代码,我们可以帮你反向操作。你能给我们看一下对象是如何创建的吗?给我们看一下序列化代码,我们可以帮你反向操作。你好,Alex,谢谢你的回复。我完全理解您使用简单数据结构的方法(例如this.whater=1;),但我的this.whater是对JS内部对象的引用。我该怎么保存这个?用新的东西更新会破坏对数据的引用。因此JSON.parse正在/应该根据ref信息创建与以前相同的对象引用?alex,我知道(我想)。同样,我不想更新引用,我想要引用本身!正如您在上面的printscreen中所看到的,我正在从后端获取我的引用,作为字段$$hash,我希望重用这个引用。这也不会有帮助,因为我有一个这些元素的数组,并且对于每个元素,我将启动一个新的RPC调用->异步->响应,应该返回到原始对象。:-)抱歉,这看起来很复杂。在我的情况下,立即更新它们将不起作用(某些单一请求