使用 id 以外的其他值

Use a different value other than id

本文关键字:其他 id 使用      更新时间:2023-09-26

所以,在余烬中,它使用id的很多来识别模型,它也在http请求中使用它,就像

http://localhost/api/users/12

有没有办法使用不同的值作为id?例如用户名?

所以它最终是这样的...

http://localhost/api/users/john99

原来我必须这样做

App.ApplicationSerializer = DS.RESTSerializer.extend({
  primaryKey: "username"
});

在尝试了很多不同的方法之后,这是唯一有效的方法。

你可以使用 Ember 中的 Route 来说明你想要使用什么,如下所示:

App.UsersitemRoute = Ember.Route.extend({
  serialize: function(model, params) {
    return { username: model.get('username') };
  }
});

在您的用户模型中,您需要执行以下操作:

App.Usersitem = DS.Model.extend({
    username: DS.attr('string'),
});

当使用上下文调用 transitionTo 以填充 URL 时,将调用此方法。