1

I have a problem with creating Ember objects from a JSON ajax data source. If I create the object the manual way, it works perfectly, and the view gets updated. If the data itself comes from a JSON ajax data call, however, it does not work. If I inspect the resulting objects, the Ember model objects does not get the correct getter and setter properties. Does anyone know why this happens?

App.AlbumView = Ember.View.extend({
    templateName:'album',
    albums:[],
    getAll:function() {
        var self = this;

        //This works!
        self.albums.push(App.Album.create({title: 'test', artist: 'test'}));

        $.post('/Rest/list/album',null,function(data) {
            $.each(data, function (index, item) {

                //This does not work?!?
                self.albums.push(App.Album.create(item));
            });
        }, 'json');
    }
});
3
  • Please do an alert on item to see what is the output. alert(item); Commented Jun 3, 2012 at 14:20
  • Is there any reason you are using a POST request rather than a GET to get data from the server Commented Jun 3, 2012 at 15:00
  • Can you put up a JSfiddle at least of the bits that work so I can have a poke at it? I'm interested but don't know enough of the rest of your setup to understand the problem fully. Commented Jun 4, 2012 at 4:48

1 Answer 1

2

You should always use embers get('variableName') and set('variableName', newValue) methods when accessing instance variables of a view. Strange things tend to happen if you don't.

Sign up to request clarification or add additional context in comments.

3 Comments

Views don't implement get and set. When I try calling them, I get 'no such function' errors.
Views implement get and set since these methods are defined in Ember.Observable, which Ember.View extends (via Ember.Object). Example: jsfiddle.net/zentralmaschine/uh74U/1
I see. Strange. Well, it seemed to work when I moved the collection to a globally accessible ArrayController, and I guess the use of get and set makes the difference. So I accept the answer. I will try to debug the view problems seperately, then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.