Voldemar Duletskiy
Voldemar Duletskiy

Reputation: 981

Move duplicated routes code in emberjs

I have two very similar routes:

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

and

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
        });
    }
});

Where should I place categories model and total_size counter, which will be available from any routes in my application?

Upvotes: 1

Views: 51

Answers (1)

Kalman
Kalman

Reputation: 8121

As @Kingpin2k pointed out in the comments, Application route is probably your best bet for storing application-wide state info, since Application route will always fire before the other routes. I believe this can be done in 1 of 2 ways.

  1. You can invoke the application model in other routes using modelFor('application')
  2. You can use the needs API as described here

Upvotes: 1

Related Questions