1

我正在尝试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>

有人可以帮我吗?

4

1 回答 1

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'
    }];
于 2013-02-12T19:16:37.263 回答