问题
I am working on a very basic ember app , but I can't seem to get it to display the belongsto model.
My user 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]
}]
My item model:
'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
}
]
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">▲</a><a {{bindAttr href="url"}}>{{title}}</a> ({{domain}})<br />
<small>{{points}} Ups -- submitted by {{user.username}}</small><br />
{{/each}}
</ul>
</script>
回答1:
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.
This should work:
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
}
];
EDIT: 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/
Hope this helped!
来源:https://stackoverflow.com/questions/16006923/ember-data-not-loading-belongsto-relationship-using-fixtures