Gal Ben-Haim
Gal Ben-Haim

Reputation: 17803

how to observe changes to a static object?

I'm trying to implement geocoding using Google maps JSON API.

I created a model for the location, and and ObjectController, the model has the async geocoding logic.

I want the controller to observe changes to the model to reflect the most update data.

I'm trying both with binding and observe and both doesn't work:

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')
});

EDIT: changing to observes('App.Location.geocoded.response')fix the issue. can it also work with binding? is there a better way to do this ?

Upvotes: 1

Views: 439

Answers (1)

sly7_7
sly7_7

Reputation: 12011

Here when you write contentBinding: 'App.Location.geocoded', the content is updated when App.Location.geocoded change. But in the success handler, you don't change the geocoded object, but just update its response property.

So, if you want to keep the binding with the geocoded object, you can try to do

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

in the ajax success handler.

Upvotes: 1

Related Questions