在emberjs中渲染belongsTo子项时出错_create

时间:2013-02-12 15:52:38

标签: ember.js

我正在尝试在mediaitems模板中呈现post,但我收到了这个令人讨厌的控制台错误:

Uncaught TypeError: Object photo has no method '_create'

这些是我的模特&夹具数据:

/**************************
 * Models
 **************************/

App.Store = DS.Store.extend({
    revision: 11,
    adapter: 'DS.FixtureAdapter'
});



App.Mediaitem = DS.Model.extend({
    type: DS.attr('string'),
    url: DS.attr('string'),

    post: DS.belongsTo('App.Post')
});



App.Post = DS.Model.extend({
    type: DS.attr('string'),
    title: DS.attr('string'),
    summary: DS.attr('string'),
    body: DS.attr('string'),
    date: DS.attr('date'),

    mediaitems: DS.hasMany('App.Mediaitem', {embedded:true})
});

App.Post.FIXTURES = [
    {
        id:"post-one", 
        type:"news", 
        title:"First Post", 
        summary:"Ipsum Lorem", 
        date:"2013-02-07T16:44:57",
        mediaitems:[{
            id:593,
            post_id:"post-one",
            type:'photo',
            url:'http://www.google.com'
        },
        {
            id:789,
            post_id:"post-one",
            type:'photo',
            url:'http://www.google.com'
        }]
    }, 
    {
        id:"post-two",
        type:"gallery", 
        title:"Second Post", 
        summary:"Lorem ipsum", 
        date:"2013-02-07T16:44:57",
        mediaitems:[{
            id:342,
            post_id:"post-two",
            type:'photo',
            url:'http://www.google.com'
        },
        {
            id:231,
            post_id:"post-two",
            type:'photo',
            url:'http://www.google.com'
        }]
    }
];

这是我的模板代码:

    <script type="text/x-handlebars" data-template-name="post">
        <div class="detail">
            {{#linkTo posts}}close{{/linkTo}}<br/>
            <h2>{{id}} - {{title}}</h2>
            <br/>
            {{#each mediaitem in mediaitems}}
                print something
            {{/each}}
        </div>
    </script>

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

FIXTURE适配器不支持嵌入式关系,至少在rev 11中不支持。 您需要每个模型都有自己的FIXTURE定义,其中包含记录和关系,以获得正确子/父的id / s。

App.Post.FIXTURES = [
{
    id:"post-one", 
    type:"news", 
    title:"First Post", 
    summary:"Ipsum Lorem", 
    date:"2013-02-07T16:44:57",
    mediaitems:['593','789']
}, 
{
    id:"post-two",
    type:"gallery", 
    title:"Second Post", 
    summary:"Lorem ipsum", 
    date:"2013-02-07T16:44:57",
    mediaitems:['342','231']
}];

App.Mediaitems.FIXTURES = [{
        id:342,
        post_id:"post-two",
        type:'photo',
        url:'http://www.google.com'
    },
    {
        id:231,
        post_id:"post-two",
        type:'photo',
        url:'http://www.google.com'
    },
    {
        id:593,
        post_id:"post-one",
        type:'photo',
        url:'http://www.google.com'
    },
    {
        id:789,
        post_id:"post-one",
        type:'photo',
        url:'http://www.google.com'
    }];