在 emberjs 中移动重复的路由代码

Move duplicated routes code in emberjs

我有两条非常相似的路线:

App.DiscountRoute = Ember.Route.extend({
    model: function() {
        return Ember.RSVP.hash({
            categories: this.store.findAll('discountCategory'),
            discounts: this.store.findAll('discount'),
            total_size: 6
        });
    }
});

App.CategoryRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render('discount');
    },
    model: function(params) {
        return Ember.RSVP.hash({
            categories: this.store.findAll('discountCategory'),
            discounts: this.store.filter('discount',function(discount) {
                return discount.get('discountCategory').get('id')==params.category_id
            }),
            total_size: 6
        });
    }
});

我应该在哪里放置类别模型和 total_size 计数器,我的应用程序中的任何路由都可以使用它们?

正如@Kingpin2k 在评论中指出的那样,Application 路由可能是存储应用程序范围内状态信息的最佳选择,因为 Application 路由总是在其他路由之前触发。我相信这可以通过两种方式中的一种来完成。

  1. 您可以使用 modelFor('application')
  2. 在其他路由中调用 application 模型
  3. 您可以按照 here
  4. 所述使用 needs API