我正在尝试学习 ember,但很难找出为什么我的后端数据没有显示出来。我将 Ember Data 与 Mirage 固定装置一起使用。数据正在显示。只有当我引入简单的关系时,来自这些关系的数据才会显示在我的应用程序中。<!---->
当您刚刚进入您的 ember 应用程序时,是否有一种通用的调试方法?
mirage/fixtures/contacts.js
export default [
{
id: 1,
userName: "Barack Obama",
profilePictureUrl: "/img/profilepics/barack.png",
conversation: 1
},
];
mirage/fixtures/conversations.js
export default [
{
id: 1,
contact: 1,
lastConversationTime: 'Sun Jun 07 2015 14:05:50 GMT+0200 (CEST)',
},
];
mirage/config.js
export default function() {
this.get('/api/conversations');
this.get('/api/conversations/:id');
this.get('/api/contacts');
this.get('/api/contacts/:id');
}
App = Ember.Application.create();
App.Router.map(function() {
// put your routes here
this.route('contacts');
this.route('conversations');
});
App.ContactsRoute = Ember.Route.extend({
model: function() {
return this.store.find('contact');
}
});
App.ConversationsRoute = Ember.Route.extend({
model: function() {
return this.store.find('conversation');
}
});
App.ContactModel = DS.Model.extend({
userName: DS.attr('string'),
profilePictureUrl: DS.attr('string'),
conversation: DS.belongsTo('conversation'),
});
App.ConversationModel = DS.Model.extend({
lastConversationTime: DS.attr('date'),
contact: DS.belongsTo('contact')
});
$.mockjax({
url: "/contacts",
type: "GET",
status: 200,
statusText: "OK",
responseText: {
contacts: [{
id: 1,
userName: "Barack Obama",
profilePictureUrl:"/img/profilepics/barack.png",
conversation: 1
}]
}
});
$.mockjax({
url: "/conversations",
type: "GET",
status: 200,
statusText: "OK",
responseText: {
conversations: [{
id: 1,
contact: 1,
lastConversationTime: 'Sun Jun 07 2015 14:05:50'
}]
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ember Starter Kit</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://builds.emberjs.com/tags/v1.11.1/ember-template-compiler.js"></script>
<script src="http://builds.emberjs.com/tags/v1.11.1/ember.debug.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.0.0-beta.16.1/ember-data.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.js"></script>
</head>
<body>
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
{{link-to 'contacts' 'contacts'}}
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="contacts">
<ul>
{{#each model as |contact|}}
<li>{{contact.userName}}</li>
<li>{{conversation.lastConversationTime}}</li>
{{/each}}
</ul>
</script>
</body>
</html>
Ember:1.12.1 Ember 数据:1.0.0-beta.19 jQuery:1.11.3
这是重现问题的 JSBin: http ://emberjs.jsbin.com/deledemaya/1/edit?html,js,output