1

我收到错误

Uncaught TypeError: Object #<Object> has no method 'map' 

当使用 ember-data 尝试从我的 api 加载 json 时。

App = Ember.Application.create({});

App.Student = DS.Model.extend({
    primaryKey: 's_id',
    s_id: DS.attr('integer'),
    surname: DS.attr('string')
});

App.Student.reopenClass({
    url: 'api/student.php'
})

App.adapter = DS.Adapter.create({
    findAll: function(store, type) {
        var url = type.url;

        console.log(type.url);

        jQuery.getJSON(url, function(data){
            store.loadMany(type, data);
        });
    }
});

App.store = DS.Store.create({
    revision: 4,
    adapter: App.adapter
});

App.students = App.store.findAll(App.Student);

我已确保 JSON 格式正确,但无济于事;有任何想法吗?

谢谢

4

2 回答 2

0

我发现在使用自定义适配器时,最好在 App.adapter 中定义所有方法(findAll、find、createRecord、createRecords 等)以避免抛出错误。你会认为逐步实现这个方法最好是控制你的应用程序中发生的事情,但是如果你这样做,默认适配器会跳进去。

还要检查 json 响应的正确格式

于 2012-08-28T23:05:03.350 回答
0

pauldechov在上面的评论中回答了这个问题:

I think I know what's going on -- DS.RestAdapter does assume the pluralized form as the root of the json in its implementation of findAll, but if you need this, you should either mimic this implementation or just use/extend that built-in one.

Because the root of the json is named, I can call data.student to get call loadMany correctly.

于 2012-08-30T08:35:12.617 回答