Lukas
Lukas

Reputation: 7734

Get array of elements with Ember.js

I have this Ajax and Ember.js call

$.ajax({
    url: '../data',
    type: 'GET',
    cache: false,
    dataType: 'json',
    data: ({
        func: "getAllLists"
    }),
    success: function(data) {
        console.log('DEBUG: GET Data OK');
        var list = [
            {
                uid: data[0].uid
            }
        ];
        App.ListRoute = Ember.Route.extend({
            model: function () {
                return list;
            }
        });
    },
    error: function() {
        console.log('DEBUG: GET Data Failed');
    }
});

this is tam

{{#each}}
<li>
    {{#link-to 'project' this}}
        {{title}}
    {{/link-to}}
</li>
{{/each}}

and i have an error: Assertion Failed: The value that #each loops over must be an Array. You passed (generated list controller). What im doing wrong?

Upvotes: 1

Views: 111

Answers (1)

chopper
chopper

Reputation: 6709

You need to do your $.ajax() call inside the App.ListRoute, not the other way around:

App.ListRoute = Ember.Route.extend({
    model: function () {
        list = $.ajax({
            url: '../data',
            type: 'GET',
            cache: false,
            dataType: 'json',
            data: ({
                func: "getAllLists"
            }),
        }).then( function(data) {
            console.log('DEBUG: GET Data OK');
            var list = [{
                uid: data[0].uid
            }];
        },);
        return list;
    }
});

Note that Ember will not enter the route until the ajax call is complete (because it will wait for the then() to resolve. Have a look at http://emberjs.com/guides/routing/asynchronous-routing/

Upvotes: 1

Related Questions