Ember.js中的这种关系有什么问题



我在基本的ember应用程序中遇到了这个错误,无法找出问题所在。

Uncaught Error: Assertion Failed: You looked up the 'author' relationship on a 'question' with id 101 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.belongsTo({ async: true }))

App.ApplicationStore = DS.Store.extend({
    adapter: DS.FixtureAdapter
});
App.Question = DS.Model.extend({
    title: DS.attr('string'),
    question: DS.attr('string'),
    author: DS.belongsTo('user')
});
App.User = DS.Model.extend({
    fullname:  DS.attr('string'),
    email:     DS.attr('string'),
    questions: DS.hasMany('question', { async: true })
});
App.Question.FIXTURES = [
    {
        id:        101,
        title:    'How do I become an expert?',
        author:    201,
        question: 'Can I become an expert in Ember?'
    },
    {
        id:        102,
        title:    'Are there lives in other planet?',
        author:    202,
        question: 'What are the chances of lives in other planets.'
    }
];
App.User.FIXTURES = [
    {
        id:         201,
        fullname:  'Ruby',
        email:     'ruby@rails',
        questions: [101]
    },
    {
        id:         202,
        fullname:  'Jruby',
        email:     'ruby@java',
        questions: [102]
    }
];

装载顺序

<script src="js/app.js"></script>
<script src="js/models/user_model.js"></script>
<script src="js/models/question_model.js"></script>
<script src="js/fixtures/question_fixtures.js"></script>
<script src="js/fixtures/user_fixtures.js"></script>

您的作者也需要异步,因为它提供了一个id,而不是一个记录。

App.Question = DS.Model.extend({
    title: DS.attr('string'),
    question: DS.attr('string'),
    author: DS.belongsTo('user', {async:true})
});

此外,不应该再定义存储,您只需要定义适配器/串行器

App.ApplicationAdapter = DS.FixtureAdapter;

最新更新