Best approach to fetch data in every state of app

*爱你&永不变心* 提交于 2019-12-25 09:45:48

问题


I've discovered this jsFiddle and I'm now wondering what the best approach would be to display the list of contributors both in the IndexRoute and in all Subroutes of match('/contributor/:contributor_id') with the new v2 Router from Ember.js.

The problem I'm experiencing is that

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return App.Contributor.find();
    }
});​

is only fetching the data on the specified / route.

When directly navigating to /#/contributor/[some_id] the data for the IndexRoute doesn't get loaded and the user would first have to navigate to / to see the list of contributors.

The only possible solution I've come to is to have a ContributorsView which gets called called in the application template. Meanwhile I don't know how to populate this view with data, as a view doesn't have the model property which the route has.


回答1:


I'm not yet confortable with the new router implementation, but from a design point of view, I would use a different structure for the routes, for example by adding a contributors route, accessible from the index, and perhaps directly redirect to contributors. Then you have always the contributors displayed, and when clicking on one of them you could see the details. A kind of master/detail blocks.

Here the router map:

App.Router.map(function(match){
  match('/').to('home');
  match('/about').to('about');
  match('/contributors').to('contributors', function(match){
    match('/').to('contributorsIndex');
    match('/:contributor_id').to('contributor', function(match){
      match('/').to('contributorIndex');
      match('/details').to('contributorDetail');
      match('/repos').to('contributorRepos');
    });
  });
});

Here is what I would do: http://jsfiddle.net/JLHuG/21/ does it work for you ?



来源:https://stackoverflow.com/questions/14125456/best-approach-to-fetch-data-in-every-state-of-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!