所以我只是在玩EmberJS而且我真的想学习EmberJS,但我已经陷入了这个问题。我正在进行api调用,我认为我已经按照EmberJS RESTAdapter
的要求格式化了json,但我并不完全确定。
我的路由器:
App.Router.map( function() {
this.route('about', { path: '/about' });
this.route('users', { path: '/users' });
});
我的用户路线:
App.UsersRoute = Ember.Route.extend({
model: function() {
/*
this.store.findAll('user').then( function( results ) {
console.log( results );
}, function() {
console.log('failed');
});
*/
return this.store.findAll('user');
}
});
我的app.js
var App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://api.diverseevolution.com'
});
App.ApplicationSerializer = DS.RESTSerializer.extend({
primaryKey: 'id',
serializeId: function( id ) {
return id.toString();
}
});
我的模板:
<script type="text/x-handlebars" data-template-name="users">
<p>This is the users route, the goal is to plugin the users fixtures.</p>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>First name:</th>
<th>Last name:</th>
</tr>
</thead>
<tbody>
{{#each model as |user|}}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.first_name }}</td>
<td>{{ user.last_name }}</td>
</tr>
{{else}}
<tr>
<td colspan="3">No users found...</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
我的模特:
App.User = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
email: DS.attr('string')
});
最后我的json响应来自该服务:
{
"users":[
{
"id":"1",
"firstName":"Thomas",
"lastName":"Bird",
"address":"4035 Stonebridge",
"city":"Holly",
"state":"MI",
"zip":"48442",
"email":"godoploid@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"1",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"1",
"title":"Admin",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
},
{
"id":"2",
"firstName":"Avery",
"lastName":"Crain",
"address":"282 Mansfield Dr.",
"city":"Lapeer",
"state":"MI",
"zip":"48446",
"email":"avery.crain@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
},
{
"id":"3",
"firstName":"Mike",
"lastName":"Marshall",
"address":"4890 Lawndale St.",
"city":"North Branch",
"state":"MI",
"zip":"48461",
"email":"mjmarshall@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
},
{
"id":"4",
"firstName":"Kylar",
"lastName":"Bird",
"address":"5875 Gracelawn",
"city":"North Branch",
"state":"MI",
"zip":"48461",
"email":"kbird@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
}
]
}
可在此处找到实时API:http://api.diverseevolution.com/users
这是我得到的错误:
我为我的生活无法弄清楚为什么我一直得到这个错误,我已经将api更改为Ember文档中描述的一些格式,我也改变了序列化器,适配器仍然可以'弄清楚为什么我会收到这个错误,如果有人能帮忙我会非常感激。我真的试图绕过EmberJS,但过去两天我一直被困在这里!
由于
答案 0 :(得分:0)
看起来我无法重现您的错误,在我看来,如果您:
first_name
中的拼写错误,而不是ApplicationSerializer
def get_values(my_dict):
sub_vals = []
actual_vals = []
for val in my_dict.values():
try:
sub_vals += get_values(val)
except AttributeError:
actual_vals += [val]
return sub_vals + actual_vals
(默认情况没有任何不同)您的代码将有效。
答案 1 :(得分:0)
似乎EmberJS不喜欢我从api发回的多重关系。
{
"id":"3",
"firstName":"Mike",
"lastName":"Marshall",
"address":"4890 Lawndale St.",
"city":"North Branch",
"state":"MI",
"zip":"48461",
"email":"mjmarshall@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
}
具体来说,status
和type
一旦我摆脱了所有工作。我现在的反应看起来像这样并完美地运作:
{
"id":"1",
"firstName":"Thomas",
"lastName":"Bird",
"address":"123 Somewhere Rd",
"city":"Somewhere",
"state":"MI",
"zip":"98675",
"email":"godoploid@gmail.com",
"status":"1",
"userTypeId":"1",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null
}