2012-02-23 79 views
1

我有一个谷歌地图的视图,我希望它公开地图,以便我可以从另一个对象绑定到它。问题是,因为我只能实例化地图上的didInsertElement(直接实例化它不会工作,因为div尚未渲染)我无法访问映射的真正值(它总是返回null)。无法访问属性在ember.js视图

这里是我的代码:

Places = Ember.Application.create(); 
Places.MapView = Ember.View.extend({ 
    tagName : 'div',  
    map : null,    
    didInsertElement : function() { 
     this._super(); 
     this.set('map', new google.maps.Map($('#map').get(0), { 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: new google.maps.LatLng(-33.8665433,151.1956316), 
     zoom: 15 
     })); 
     this.get('map'); // Returns the value of the map OK 
    } 
}); 

Places.searchController = Ember.Object.create({ 
    mapBinding: 'Places.MapView.map' // Doesn't work 
} 

这里是模板:

<script type="text/x-handlebars"> 
{{#view Places.MapView id="map"}}{{/view}}   
</script> 

如果我设置内MapView任何其他财产直接我没有问题,结合它。有关如何解决这个问题的任何想法?

回答

5

在我的控制器中,我通常绑定到“模型”对象而不是视图对象。

也许尝试将地图设置为不同的对象。

Places = Ember.Application.create(); 

Places.MapData = Ember.Object.create({ 
    map: null 
}); 

Places.MapView = Ember.View.extend({ 
    tagName : 'div',  
    map : null,    
    didInsertElement : function() { 
     this._super(); 
     Places.MapData.set('map', new google.maps.Map($('#map').get(0), { 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: new google.maps.LatLng(-33.8665433,151.1956316), 
     zoom: 15 
     })); 
    } 
}); 

Places.searchController = Ember.Object.create({ 
    mapBinding: 'Places.MapData.map' 
}); 
1

在你searchController您绑定到的MapView类的地图属性。该课程没有地图属性。当该类被实例化并且运行时,地图属性被添加到MapView实例。为了使你的绑定工作,你需要绑定到实例化视图的map属性,而不是类。

+0

我试图'Places.mapView = Places.MapView.create({})'和'{{#view Places.mapView ID = “地图”}} {{/视图}}'但随后的地图没有出现在屏幕上。 – Johnny 2012-02-23 13:58:43