lundi 22 février 2016

Ember setting up routes to get associated data from rails api

I have an Ember app setup that gets it's data from a Rails API. I have routes setup right now that work, but not how I'm wanting. The current route structure (for the ember app) is this:

//router.js
Router.map(function() {
  this.route('surveys');
  this.route('survey', { path: 'surveys/:survey_id' }, function() {
    this.route('sections', { path: '/sections });
  });

  this.route('section', {path: 'surveys/:survey_id/sections/:section_id'}, function() {
    this.route('survey_items', { path: 'survey-items });
  });

});

In the models I have surveys, and each survey has many sections, and each section has many survey items. The way it's setup now kinda work, but my sticking point is, if I go to the section route I want to have a "next" button that when you click it, it takes you to the next section in that survey. Right now I can't make that happen correctly, because my section route loads the model by just finding the section by the passed in id param. If all of the sections for all surveys are loaded in the store already and I increment the section right now, it goes to the next section regardless of which survey_id it is associated with. I can't figure out how to setup my routes better to make this work.

The rails api routes look like this right now:

routes.rb
namespace :api do
  namespace :v1 do
    resources :surveys, shallow:true do
      resources :sections, shallow: true do
        resources :survey_items, path: '/survey-items'
      end
    end
  end
end

As you can see, in the Rails routes I put shallow: true on the surveys and sections resources. For some reason, when I had them nested without shallow routes, I couldn't get ember to load the proper route without nesting my ember routes, which nests the UI, which I don't want. Can anyone give me any tips on how I can setup my routes without needing shallow routes in rails (if possible) and how I should be getting the model data for each section so the I can go to the next section in the associated survey?

The current routes model for section is fetched like this currently:

model(params) {
  return this.store.findRecord('section', params.section_id, { reload: true });

}




Aucun commentaire:

Enregistrer un commentaire