当保存嵌套资源(例如保存属于属性的突出显示)时,当我需要“POST”到“api/v1/properties/properties”时,Ember似乎不会从嵌套路由层次结构中拾取“POST”到“api/v1/高亮显示”。因此,我考虑不嵌套后端的路由来解决它,并以某种方式通过property_id,但是让它能够理解正确的路线要容易得多。我偶然看到了buildURL混音器,每次都要构建正确的路径,我想知道这是否是最好的方法,我做错了什么吗?
谢谢你提前提供的帮助
这是我代码的一部分..。
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结束
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');
});
});
});
});适配器
import Ember from 'ember';
export default Ember.Mixin.create({
host: 'http://localhost:3000',
namespace: 'api/v1'
});highlight.js中的突出显示模型
property: DS.belongsTo('property', {async: true }),property.js中的属性模型
highlights: DS.hasMany('highlight', {async: true }),发布于 2015-07-12 12:56:04
当使用成员数据时的一般惯例是,每个模型端点是彼此的兄弟姐妹。
如果您真的想要这样做,您必须构建自己的适配器,这比按照约定做事情要费劲得多。
所以你的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模型/荧光.
export default Model.extend({
property: DS.belongsTo('property', {async: true })
})模型/Property.js
export default Model.extend({
property: DS.hasMany('highlight', {async: true })
})模型/备选案文.
export default Model.extend({
property: DS.belongsTo('highlight', {async: true })
})例如,属性控制器中的操作。
save:function(){
highlight.save().then(function(highlight){
property.get(highlights).pushObject(highlight);
property.save();
});
}当property.save执行时,适配器将自动将highlight_ids :[1]添加到它发送给服务器的有效负载中。
https://stackoverflow.com/questions/31358823
复制相似问题