谷歌放置自动完成,如何清理pac容器? [英] Google places autocomplete, how to clean up pac-container?

查看:104
本文介绍了谷歌放置自动完成,如何清理pac容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用google places autocomplete控件,它会使用类 pac-container 为下拉列表创建一个元素。

I'm using the google places autocomplete control, and it creates an element for the drop down with a class pac-container.

我在ember应用程序中使用自动完成功能,当我完成DOM元素时,自动完成必然会被删除,但是 pac-container 元素仍然存在,甚至认为它是隐藏的。下次我实例化一个新的自动完成时,会创建一个新的 pac-container 并保留旧的。我似乎无法在API上找到类似于dispose方法的内容,那么有没有正确的方法呢?如果不是,我想我应该只使用jquery来清理元素。

I'm using the autocomplete in an ember app, and when I'm done with it the DOM element the autocomplete is bound to gets removed, but the pac-container element remains, even thought its hidden. Next time I instantiate a new autocomplete, a new pac-container is created and the old one remains. I can't seem to find anything like a dispose method on the API, so is there a way of doing this correctly? If not I guess I should just use jquery to clear up the elements.

推荐答案

我遇到了同样的问题,希望谷歌最终提供了一个官方的清理方法,但是现在我能够通过手动删除pac-container对象来解决问题,这个对象可以在返回的Autocomplete类中找到:

I was having the same problem, and hopefully Google eventually provides an official means of cleanup, but for now I was able to solve the problem by manually removing the pac-container object, a reference to which can be found in the Autocomplete class returned from:

var autocomplete = new google.maps.places.Autocomplete(element, options);

可以在以下位置找到对pac容器元素的引用:

The reference to the pac-container element can be found at:

autocomplete.gm_accessors_.place.Mc.gm_accessors_.input.Mc.L

我只是从我的widget析构函数中删除了DOM:

Which I simply removed from the DOM in my widget destructor:

$(autocomplete.gm_accessors_.place.Mc.gm_accessors_.input.Mc.L).remove();

希望这有帮助。

更新

我不确定Google的混淆是如何工作的,但以上部分内容似乎模糊不清,如果API的混淆或内部结构发生变化,显然会失败。对后者无能为力,但对于前者,您至少可以按预期标准搜索对象属性。正如我们所看到的,一些属性名称没有被混淆,而一些属性名称似乎是,例如Mc和L。为了使其更加健壮,我编写了以下代码:

I'm not sure how Google's obfuscation works, but parts of the above seem obfuscated, and obviously will fail if the obfuscation or internal structures of the API change. Can't do much about the latter, but for the former you could at least search the object properties by expected criteria. As we can see, some of the property names are not obfuscated, while some appear to be, such as "Mc" and "L". To make this a little more robust, I wrote the following code:

var obj = autocomplete.gm_accessors_.place;
$.each(Object.keys(obj), function(i, key) {
  if(typeof(obj[key]) == "object" && obj[key].hasOwnProperty("gm_accessors_")) {
    obj = obj[key].gm_accessors_.input[key];
    return false;
  }
});
$.each(Object.keys(obj), function(i, key) {
  if($(obj[key]).hasClass("pac-container")) {
    obj = obj[key];
    return false;
  }
});
$(obj).remove();

代码期望一般结构保持不变,而不依赖于(可能)混淆的名称Mc和L。丑陋我知道,但希望谷歌很快解决这个问题。

The code expects the general structure to remain the same, while not relying on the (possibly) obfuscated names "Mc" and "L". Ugly I know, but hopefully Google fixes this issue soon.

这篇关于谷歌放置自动完成,如何清理pac容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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