[英]How can I have a method in an Object return an object with a dynamic value?
在我的Ember应用程序中,我正在尝试为Google Maps服务创建包装。 为了设置地图的参数,我必须创建一个包含所有参数的对象。 我正在尝试在mapOptions属性中执行此操作。 理想情况下,我想在对象内部使用小的辅助方法来计算Google Maps选项对象的值,但是当我尝试设置对象的center属性时却遇到“未定义不是函数”错误在我的MapBackgroundGenerator对象中创建对象。 我假设它是一个范围问题,但是我不确定如何在对象创建方法中访问我的帮助器方法。
App.MapBackgroundGenerator = Ember.Object.extend
placesService: null
placeId: null
mapContainer: null
setMapLocationFrom: (placeId) ->
console.log(placeId)
@get('placesService').getDetails(placeId: placeId, (place, status) =>
console.log(place)
# @set('placeLocation', place.geometry.location)
return place.geometry.location
)
placeLocation: 1111
# ).observes('placeId')
mapOptions: # ()->
# console.log(@get('placeLocation'))
# return {
zoom: 8
center: @get('placeLocation')
# }
# mapHeight: null
# mapWidth: null
map: null
init: ()->
console.log('init')
@._super
@set('mapContainer', @get('mapContainer')[0])
@set('placesService',
new google.maps.places.PlacesService(@get('mapContainer')))
console.log(@get('mapOptions'))
@set('map',
new google.maps.Map(@get('mapContainer'), @get('mapOptions')))
@get
必须在实例的上下文中执行。 在这里,您正在窗口的上下文中执行它。
您可以将mapOptions更改为计算属性:
mapOptions: function(){
return {
zoom: 8
center: this.get('placeLocation')
}
}.property('placeLocation')
或者,如果仅希望将其用作方法,则不会使用getter
,而只需调用func
mapOptions: function(){
return {
zoom: 8
center: this.get('placeLocation')
}
}
@set('map', new google.maps.Map(@get('mapContainer'), @mapOptions()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.