1

我正在尝试使用谷歌地图 JSON API 实现地理编码。

我为位置和 ObjectController 创建了一个模型,该模型具有异步地理编码逻辑。

我希望控制器观察模型的变化以反映最新的数据。

我正在尝试绑定和观察,但两者都不起作用:

App.Location = Ember.Object.extend({
    formatted_address: null,
    location: {
        lat: null,
        lng: null
    }
})

App.Location.reopenClass({
    geocoded: Ember.Object.create(),
    geocode: function(address) {
        $.ajax({
            url: 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' + address,
            dataType: 'json',
            context: this,
            success: function(response) {
                App.Location.geocoded.set('response', response);
            }
        });

        return this.geocoded;
    }
});

App.LocationController = Ember.ObjectController.extend({
    contentBinding: 'App.Location.geocoded',
    testProperty: function() {
        console.log('test');    
    }.property('content'),
    testObserve: function() {
        console.log('test');
    }.observes('App.Location.geocoded')
});

编辑:更改以observes('App.Location.geocoded.response')解决问题。它也可以与绑定一起使用吗?有一个更好的方法吗 ?

4

1 回答 1

1

这里当你写contentBinding: 'App.Location.geocoded'的时候,内容会随着 App.Location.geocoded 的变化而更新。但是在成功处理程序中,您不会更改地理编码对象,而只是更新其响应属性。

所以,如果你想保持与地理编码对象的绑定,你可以尝试做

App.Location.set('geocoded', Ember.Object.create({response: response}));

在 ajax 成功处理程序中。

于 2012-12-13T13:03:04.250 回答