1

Best Practices with Deep Runs in Arms with Fog Data

 3 years ago
source link: https://www.codesd.com/item/best-practices-with-deep-runs-in-arms-with-fog-data.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Best Practices with Deep Runs in Arms with Fog Data

advertisements

I have a router which looks like that:

App.Router.map ->
  @resource 'categories', ->
    @resource 'category', path: ':category_id', ->
      @resource 'composites', ->
        @resource 'composite', path: 'composite_id, ->
          @resource 'questions', ->
            #...

All plural routes have a model hook loading all the models depending on the parent (except the root resource loading all categories). Like composites model hook will do something like App.Composite.find category_id: @modelFor('category').get('id').

So this is working but the thing I can't make work fully perfectly after trying many ways is the auto redirecting. I want that once the categories have been loaded, to redirect to the category route using the firstObject loaded by categories route, and then redirecting to the composites route, and then redirecting to the composite route with firstObjevt loaded in composites route, etc.. until the end level.

I made it partially working using redirect, firstObjectObserver, ... but I often encounter a problem with infinite redirect, or not having the models loaded before trying to redirect to the next route with first model, ...

What is the best practice to do so? Should I rewrote my router in another way (I want to e able to have an URL for each level tho even if at te end it'll redirect automatically to the last level, tho the models can change)? How should I wait for the plural route models to load before redirecting?


What is the best practice to do so? Should I rewrote my router in another way

I would recommend flattening these resources - don't let the url structure dictate the route nesting, instead nest resources based on your templates and use the path property to specify the route/resource url. So unless your UI really has a very deeply nested master-detail template with categories, composites and questions, something like this would make more sense:

App.Router.map ->
  @resource 'categories'
  @resource 'category', path: '/categories/:category_id', ->
    @resource 'composites'
    @resource 'composite', path: 'composites/:composite_id, ->
      @resource 'questions'
        #...

How should I wait for the plural route models to load before redirecting?

Either way, redirecting should work something like this:

App.CategoriesRoute = Ember.Route.extend({
  model: function() {
    return App.Category.find({});
  }
});
App.CategoriesIndexRoute = Ember.Route.extend({
  redirect: function() {
    var category = this.modelFor('categories').get('firstObject');
    this.transitionTo('category', category);
  }
});

Is that approach not working? Perhaps this will do the trick:

App.CategoriesIndexRoute = Ember.Route.extend({
  redirect: function() {
    return App.Category.find({}).then(function(results) {
      var category = results.get('firstObject');
      return this.transitionTo('category', category);
    });
  }
});


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK