解构一个开放的图层3地图

Hen*_*rik 8 javascript ember.js openlayers-3

所以,我正在使用Open Layers 3和Ember.js制作一个仪表板,我已经动态地加载了地图,但我希望它在我离开路线时被销毁,我发现的唯一一件事就是map.destroy()但是对于旧版本的API而言,新版本似乎没有.

我在进入地图页面几次后使用了chrome调试器,发现我有29个ol.Map对象.

这就是我到目前为止所拥有的

App.MapView = Ember.View.extend({
  map: null,
  didInsertElement: function() {
    this.map = new ol.Map({
      target: 'map',
      layers: [
        new ol.layer.Tile({
          source: new ol.source.MapQuest({layer: 'sat'})
        })
      ],
      view: new ol.View({
        center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
        zoom: 4
      })
    });
  },
  willDestroyElement: function() {
    // destroy this.map
  }
});
Run Code Online (Sandbox Code Playgroud)

我无法在有关删除地图的文档中找到任何内容.

提前致谢.

Hua*_*afu 20

你应该尝试做这样的事情:

App.MapView = Ember.View.extend({
  // if you are not using Ember.get/set you'd better make this "private"
  _map: null,
  didInsertElement: function() {
    this._map = new ol.Map(...);
  },
  willDestroyElement: function() {
    this._map.setTarget(null);
    this._map = null;
  }
});
Run Code Online (Sandbox Code Playgroud)

它将地图与其元素分离,并允许正确的垃圾收集.如果有的话,不要忘记删除对地图对象的任何其他引用.