无法访问ember.js视图中的属性

Joh*_*nny 1 google-maps ember.js

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

这是我的代码:

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
}
Run Code Online (Sandbox Code Playgroud)

这是模板:

<script type="text/x-handlebars">
{{#view Places.MapView id="map"}}{{/view}}          
</script>
Run Code Online (Sandbox Code Playgroud)

如果我MapView直接设置任何其他属性我没有问题绑定到它.关于如何解决这个问题的任何想法?

Vee*_*ebs 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'
});
Run Code Online (Sandbox Code Playgroud)