Ember数据不使用fixtures加载属性关系 [英] Ember data not loading belongsto relationship using fixtures

查看:94
本文介绍了Ember数据不使用fixtures加载属性关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个非常基本的ember应用程序,但我似乎无法让它显示属性模型。

I am working on a very basic ember app , but I can't seem to get it to display the belongsto model.

我的用户模型:

News.User = DS.Model.extend({
    username: DS.attr('string'),
    items: DS.hasMany('News.Item')
});

News.User.FIXTURES = [
    {
        "id": 1,
        "username": "Bobba_Fett",
        "items": [1,2]
    }]

我的项目模型:

'use strict';

News.Item = DS.Model.extend({
    title: DS.attr('string'),
    url: DS.attr('string'),
    domain: DS.attr('string'),
    points: DS.attr('number'),
    comments: DS.hasMany('News.Comment'),
    user: DS.belongsTo('News.User')
});

News.Item.FIXTURES = [
    {
        "id": 1,
        "title": "This is story 1",
        "url": "http://google.com",
        "domain": "google.com",
        "points": 45,
        "comments": [1,2,3],
        "user_id": 1
    }, 
    {
        "id": 2,
        "title": "This is another story",
        "url": "http://yahoo.com",
        "domain": "yahoo.com",
        "points": 30,
        "comments": [1,2,3],
        "user_id": 1
    }
]

我的HTML(问题是用户名不显示,只是空白),其他一切都显示正常:

My html ( the problem is that the username is not displaying, it is just blank ), Everything else displays fine:

<script type="text/x-handlebars" data-template-name="items">
        <div class="row">
            <ul>
                {{#each itemList}}
                    <a href="upvote">&#x25B2;</a><a {{bindAttr href="url"}}>{{title}}</a> ({{domain}})<br />
            <small>{{points}} Ups -- submitted by {{user.username}}</small><br />
                {{/each}}
            </ul>
        </script>


推荐答案

问题出在 news.Item.FIXTURES 声明,user_id应该被命名为user ,因为这是模型定义中关系的属性的名称。

The problem is in the News.Item.FIXTURES declaration, "user_id" should be named just "user", because that is the name the attribute for the relationship got in the Model definition.

这应该可以工作:

News.Item.FIXTURES = [
    {
        id: 1,
        title: "This is story 1",
        url: "http://google.com",
        domain: "google.com",
        points: 45,
        comment": [1,2,3],
        user: 1
    }, 
    {
        id: 2,
        title: "This is another story",
        url: "http://yahoo.com",
        domain: "yahoo.com",
        points: 30,
        comments: [1,2,3],
        user: 1
    }
];

编辑:我不知道你还在看进入这个,但这里是一个工作的小提琴哟ur代码,请注意,我不得不添加一个空模型 News.Comments = DS.Model.extend({}); 使其工作:

I don't know if you're still looking into this, but here is a working fiddle with your code, please note I had to add an empty model News.Comments = DS.Model.extend({}); for it to work:

http://jsfiddle.net/Mftye/1/

希望这有帮助!

这篇关于Ember数据不使用fixtures加载属性关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆