2014-09-23 16 views
8

所以,我使用Open Ember.js和Ember.js来创建一个仪表板,并且我已经动态加载了地图,但是我希望它在我离开路线时被破坏,唯一的我发现的东西是map.destroy(),但它的旧版本的API,似乎并没有在新版本中。解构一个Open Layer 3地图

我在转到地图页面几次后使用了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 
    } 
}); 

我不能找到有关删除地图中的文档任何东西。

在此先感谢。

回答

18

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

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; 
    } 
}); 

松脱,从它的元素的地图,并允许正确的垃圾收集。如果有的话,不要忘记删除对地图对象的任何其他引用。

+5

setTarget(null) - 谢谢! – 2015-02-24 16:35:04