简体   繁体   English

如何在JSON字符串中传递“this”,然后需要将其转换为JS Obj

[英]How to pass “this” in a JSON string which then needs to be converted to an JS Obj

I am building a small JS method where I attach data-* attrs to a link, bind a event to the link and send the data-* values to a 3rd party JS analytics API. 我正在构建一个小的JS方法,我将data-* attrs附加到链接,将事件绑定到链接并将data-*值发送到第三方JS分析API。

<a href="#" class="js-analytics-proxy" data-trackme='{"obj": this, "rmethod": "track", "partial-path": "/foo/bar" }'

And in a JS file 并在一个JS文件中

$('js-analytics-proxy').live('click', function() { 
    3rdParty.webTrack('site1', 'foo', $(this).data('trackme'));
});

The problem is: 问题是:

  1. By default, $(this).data('trackme') will return a String 默认情况下, $(this).data('trackme')将返回一个String
  2. If I $.parseJSON(..) it, it will fail because "obj": this is not a valid key/val pair in JSON string notation. 如果它是$.parseJSON(..)它,它将失败,因为"obj": this不是JSON字符串表示法中的有效键/值对。
  3. No matter what, when passed "thought" the data-* attr, only the String representation of of the JS obj name is passed. 无论如何,当传递“思考” data-* attr时,只传递JS obj名称的String表示。

I do realize that data-* are for "data" and arguable this is not data, but code, so it doesn't belong in it to begin with. 我确实认识到data-*是针对“数据”而且可以说this不是数据,而是代码,所以它不属于它的开头。 Also, I would like to avoid "hardcoding" the obj: this key/val pair into my JS class (basically taking it out of the data-* attr, and adding it to the args in the JS method if possible. 另外,我想避免将obj:hardcoding“硬编码”到我的JS类中(基本上将它从data- * attr中取出,并在可能的情况下将其添加到JS方法中的args中)。


Update 1: I am using this because the 3rdParty JS library expects it. 更新1:我正在使用它,因为3rdParty JS库需要它。 The current implementation has 目前的实施有

onclick='3rdParty.webTrack("site1", "foo", {"obj": this, "rmethod": "track", "partial-path": "/foo/bar" }' 

all over the markup. 整个标记。 We are looking at a less obtrusive approach. 我们正在寻找一种不那么突兀的方法。 Sometimes there are other key/val pairs which pass other JS objects in as well. 有时还有其他键/ val对也可以传递其他JS对象。 The obj: this was an isolated example to illustrate the problem obj:这是一个用来说明问题的孤立例子


Update 2: This seems dangerous, but I could create a convention where properties prefixed with "js:" will have their string value eval'd. 更新2:这似乎很危险,但我可以创建一个约定,其中以“js:”为前缀的属性将使其字符串值为eval'd。 { "js:obj": "this" } would be transformed to { "obj": this ... of course this comes with all the dangers of using eval, which is something I would like to avoid at all cost. { "js:obj": "this" }将被转换为{ "obj": this ...当然这带来了使用eval的所有危险,这是我想不惜一切代价避免的。

I could also simply change any String value of "this" to the current scope this which would be less dangerous, but IMO not very elegant. 我也可以简单的“本”的任何字符串值更改为当前范围this这将是危险性较低,但IMO不是很优雅。

jQuery.data() can store objects . jQuery.data()可以存储对象 Assign them using data() so jQuery can store them as objects. 使用data()分配它们,以便jQuery可以将它们存储为对象。 Also, retrieve them using data() as well. 此外,还使用data()检索它们。

//assign them via data()
$(element).data('trackme',{
    data : 'foo'
    ...
});

//retrieve them using data()
$(element).data('trackme');

Simply remove "obj": this , it's not json and won't work. 只需删除"obj": this不是json,也不会起作用。

After that, it will become legit json and the following will work, given that ThirdParty.webTrack actually expects a DOM element: 在那之后,它将成为合法的json,并且以下将起作用,因为ThirdParty.webTrack实际上需要一个DOM元素:

$('js-analytics-proxy').live('click', function() {
    var data = $.parseJSON( $(this).data( "trackme" ) );
    data.obj = this;
    ThirdParty.webTrack('site1', 'foo', data);
});

When you bind the click to the event, you can grab the reference to this and add it to the object that you are trying to pass, maybe using something like this: 当您将单击绑定到事件时,您可以获取this的引用并将其添加到您尝试传递的对象,可能使用以下内容:

$('js-analytics-proxy').live('click', function() { 
    3rdParty.webTrack('site1', 'foo', $.extend(
        $.parseJSON($(this).data('trackme')), 
        { 'obj' : this }
    );
});

Obviously, you would also remove the "obj": this part from the data-trackme attribute of the a element. 显然,你也会删除"obj": this部分来自a元素的data-trackme属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM