我正在努力,为了更好地缺少一个术语,使用固定装置来管理我的应用程序的用户模型。我一直关注fixtures, as well as associated links for model development上的文档。问题是我一直遇到cannot GET /user/Administrator
我曾经使用过yeoman来生成一个余烬项目,但是让我们来看看我的内容:
设置应用
var EW = window.EW = Ember.Application.create();
设置灯具(store.js)
EW.ApplicationAdapter = DS.FixtureAdapter;
/* I need a fixture. */
EW.User.FIXTURES = [
{
"user":{
"id":1,
"first_name":"Adam",
"email":"adamkylebalan@email.com",
"last_name":"Balan",
"user_name": "Administrator",
"role_names":[
"Administrator"
],
"blogs":[
{
"id":2,
"title":"Blog Title",
"posts":[
{
"id":1,
"title":"Something",
"content":"Some Content",
"tag_names":[
"Some Tag",
"Other Tag"
],
"category_names":[
"Some Category",
"Another Category"
],
"blog_id":2,
"comments":[
{
"id":1,
"post_id":1,
"author":"Adam",
"comment":"This is a valid long comment."
}
]
}
]
}
]
}
}
]
模型
var attr = DS.attr;
// Set up the user model and it's relationships.
EW.User = DS.Model.extend({
first_name: attr('string'),
last_name: attr('string'),
user_name: attr('string'),
blogs: DS.hasMany('blog'),
role: DS.hasMany('roles'),
// Create full names for every one.
fullName: function(){
return this.get('first_name') + ' ' + this.get('last_name');
}.property('first_name', 'last_name')
});
路由器
EW.Router.map(function(){
this.resource('user', { path: '/user/:user_name' });
});
EW.UserRoute = Ember.Route.extend({
model: function(params){
return jQuery.getJSON("/user/" + params.user_name);
}
serialize: function(model){
return { user_name: model.get('user_name') }
}
});
据我所知,我已经做好了一切。抛出错误:
Cannot GET /user/Administrator
我不确定我是否未能正确设置。控制台没有给我任何错误,因为这个错误导致ember控制台无法加载,网络选项卡给我localhost:9000/user/Administrator
想法?
答案 0 :(得分:1)
您需要在模型钩子中调用return this.get('store').find('user', params.user_id)
:
EW.Router.map(function() {
this.resource('user', { path: '/users/:user_id' });
});
EW.UserRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('user', params.user_id);
}
});
请注意,它是user_id
而不是user_name
。这是因为夹具适配器被硬编码以按id
而不是名称查找记录。我建议使用名称的网址友好形式作为ID。