且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用“ember-rails”将路径从Rails迁移到Ember与现有Rails应用程序

更新时间:2023-02-03 18:04:02

如果您正在使用ember数据使用休息适配器,配置如下:

If you are using the ember data with rest adapter the configuration is the following:

给定此url your-host / api / v1 / newslinks with以下json结构:

Given this url your-host/api/v1/newslinks with the following json structure:

{
  newslinks: [
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar' }
  ]
}

您只需要映射新闻链路由:

You just need to map the newslinks routing:

App.Router.map(function() {
    this.resource('newslinks', { path: '/' });
});

并将命名空间映射到 DS.RestAdapter

And map the namespace in DS.RestAdapter:

DS.RESTAdapter.reopen({
  namespace: 'api/v1'
});

这里是使用静态适配器并嘲笑响应的现场演示。

Here is a live demo using rest adapter and mocking the response.

默认情况下,rails将服务于没有json根路径的json:

By default rails will serve the json without the root path of the json:

[
  {id: 1, name: 'foo'},
  {id: 2, name: 'bar' }
]

为了轻松实现这项工作,只需将:json 添加到 render 方法中,然后再输入数据。因此,rails将使用活动模型序列化程序,根路径将出现:

To get this work easily, in a rails controller just add the :json to your render method, followed by the data. So rails will use the active model serializers, and the root path will be present:

def index
  @users = User.all
  render json: @users
end

我希望它有帮助。