2012-02-23 83 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