不了解ember中的灯具

时间:2014-05-06 18:47:13

标签: ember.js

我正在努力,为了更好地缺少一个术语,使用固定装置来管理我的应用程序的用户模型。我一直关注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

的404

想法?

1 个答案:

答案 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。