首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >POST 'POST‘嵌套路由

POST 'POST‘嵌套路由
EN

Stack Overflow用户
提问于 2015-07-11 15:37:57
回答 1查看 232关注 0票数 0

当保存嵌套资源(例如保存属于属性的突出显示)时,当我需要“POST”到“api/v1/properties/properties”时,Ember似乎不会从嵌套路由层次结构中拾取“POST”到“api/v1/高亮显示”。因此,我考虑不嵌套后端的路由来解决它,并以某种方式通过property_id,但是让它能够理解正确的路线要容易得多。我偶然看到了buildURL混音器,每次都要构建正确的路径,我想知道这是否是最好的方法,我做错了什么吗?

谢谢你提前提供的帮助

这是我代码的一部分..。

routes.rb

代码语言:javascript
复制
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

结束

router.js

代码语言:javascript
复制
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');
        });
      });
    });
  });

适配器

代码语言:javascript
复制
import Ember from 'ember';

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

highlight.js中的突出显示模型

代码语言:javascript
复制
property: DS.belongsTo('property', {async: true }),

property.js中的属性模型

代码语言:javascript
复制
highlights: DS.hasMany('highlight', {async: true }),
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-12 12:56:04

当使用成员数据时的一般惯例是,每个模型端点是彼此的兄弟姐妹。

如果您真的想要这样做,您必须构建自己的适配器,这比按照约定做事情要费劲得多。

所以你的routes.rb看起来就像

代码语言:javascript
复制
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

模型/荧光.

代码语言:javascript
复制
export default Model.extend({
  property: DS.belongsTo('property', {async: true })
})

模型/Property.js

代码语言:javascript
复制
export default Model.extend({
  property: DS.hasMany('highlight', {async: true })
})

模型/备选案文.

代码语言:javascript
复制
export default Model.extend({
  property: DS.belongsTo('highlight', {async: true })
})

例如,属性控制器中的操作。

代码语言:javascript
复制
save:function(){
   highlight.save().then(function(highlight){
     property.get(highlights).pushObject(highlight);
     property.save();
   });
}

当property.save执行时,适配器将自动将highlight_ids :[1]添加到它发送给服务器的有效负载中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31358823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档