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'. 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. 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?
Thanks for all the help in advance
Here are parts of my code...
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
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
property: DS.belongsTo('property', {async: true }),
property model in 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.
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
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
export default Model.extend({
property: DS.belongsTo('property', {async: true })
})
models/property.js
export default Model.extend({
property: DS.hasMany('highlight', {async: true })
})
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments