简体   繁体   English

Ember Rails的“ POST”嵌套路线

[英]Ember Rails 'POST' Nested Route

When saving nested resources (ex saving a highlight that belongs to a property) it seems that Ember doesn't pickup on the nested route hierarchy and 'POSTS' to just 'api/v1/highlights' when i need it to 'POST' to 'api/v1/properties/property_id/highlights'. 当保存嵌套资源时(例如保存属于属性的高亮显示),Ember似乎不选择嵌套路由层次结构,而当我需要将其“ POST”到“ api / v1 / highlights”时将其“ POSTS”保存为“ api / v1 / highlights” 'api / v1 / properties / property_id / highlights'。 So I thought about un-nesting the routes on the back-end to solve it and passing the property_id somehow, but it would be a lot easier to just be able to have it understand the correct route. 因此,我考虑取消后端的路由以解决该问题并以某种方式传递property_id,但是能够使它理解正确的路由会容易得多。 I came across the buildURL mixin to build the correct path for each time and was wondering if that's the best way to do it and is there something I'm doing wrong? 我遇到了buildURL mixin每次都构建正确的路径,并且想知道这是否是最好的方式,是否有做错的地方?

Thanks for all the help in advance 预先感谢您的所有帮助

Here are parts of my code... 这是我的代码的一部分...

routes.rb routes.rb

namespace :api, defaults: { format: :json } do
namespace :v1 do
  resources :users, only: [:show, :update] 
  resources :user_devices, only: [:show]
  resources :properties do

    resources :highlights do
      resources :options
    end
  end 
  resources :fields
end

end 结束

router.js router.js

this.route('properties', function() {
this.route('new');

this.route('property', {path: ':property_id'}, function() {
  this.route('edit');
  this.route('details');

  this.route('highlights', function() {
    this.route('new');

    this.route('highlight', {path: ':highlight_id'}, function() {
      this.route('edit');

      this.route('options', function() {
        this.route('new');

        this.route('option', {path: ':option_id'}, function() {
          this.route('edit');
        });
      });
    });
  });

adapter 适配器

import Ember from 'ember';

export default Ember.Mixin.create({
  host: 'http://localhost:3000',
  namespace: 'api/v1'
});

highlight model in highlight.js Highlight.js中的高亮模型

property: DS.belongsTo('property', {async: true }),

property model in property.js property.js中的属性模型

highlights: DS.hasMany('highlight', {async: true }),

The general convention when using ember-data is that each models endpoints are siblings of each other. 使用ember-data时的常规约定是,每个模型端点都是彼此的同级。

If you really wanted to do this you would have to build your own adapter, which is much more effort then doing things according to convention. 如果您确实想执行此操作,则必须构建自己的适配器,这比按照惯例进行操作要花费更多的精力。

so your routes.rb would look like 所以你的routes.rb看起来像

namespace :api, defaults: { format: :json } do
  namespace :v1 do
    resources :users, only: [:show, :update] 
    resources :user_devices, only: [:show]
    resources :properties
    resources :highlights
    resources :options
  end 
  resources :fields
end

models/highlights.js models / highlights.js

export default Model.extend({
  property: DS.belongsTo('property', {async: true })
})

models/property.js models / property.js

export default Model.extend({
  property: DS.hasMany('highlight', {async: true })
})

models/options.js models / options.js

export default Model.extend({
  property: DS.belongsTo('highlight', {async: true })
})

action in property controller for example 例如在属性控制器中执行操作

save:function(){
   highlight.save().then(function(highlight){
     property.get(highlights).pushObject(highlight);
     property.save();
   });
}

when property.save executes, your adapter will automatically add highlight_ids :[1] to the payload it sends to the server 当执行property.save时,您的适配器将自动将highlight_ids :[1]添加到它发送给服务器的有效负载中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM