使用Ember.js获取元素数组

时间:2014-02-15 13:41:31

标签: javascript ajax ember.js

我有这个Ajax和Ember.js调用

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

这是tam

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

我有一个错误:Assertion Failed: The value that #each loops over must be an Array. You passed (generated list controller)。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要在$.ajax()内进行App.ListRoute来电,而不是相反:

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

请注意,在ajax调用完成之前,Ember不会进入路径(因为它会等待then()解析。请查看http://emberjs.com/guides/routing/asynchronous-routing/