Hey there, I’m still pretty new to Ember, and attempting to get some of the basics down when it comes to how to structure an application. I’m working on a sample app that’ll be used to teach people Ember, so I want to make sure I’m going down the right path though!
Here’s the rundown: On the index route for this site, we’re displaying the number of products (Product model, using fixtures) in the system. We’re also display 3 products that are onSale.
There are also routes for products and product which are straightforward enough – master/detail view, so no concerns there. The question is how to structure things for the index route.
Here’s what I have so far:
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('product');
}
});
App.IndexController = Ember.ArrayController.extend({
onSale: function() {
return this.filterProperty('isOnSale', true).slice(0,3);
}.property('@each.isOnSale'),
productCount: function() {
return this.get('length');
}.property('length')
});
Few questions on this:
- It feels a bit dirty to be fetching all products here, as well as in the
productsroute. Would usingmodelForhere and having theproductsroute be the main location that stores all products be a better convention? Would that run into any loading issues? - Should the route be doing the filtering rather than the controller? It seems like if this was hitting a real API, the filtering would be done on the server and the
modelwould just be an array of 3 products that are on sale. - These two properties – an array of products on sale and the number of total products – seem to be too different to be in the same controller. Wondering if it makes sense to split this out of the
indexroute altogether. I’m not sure what that would look like though. AnOnSaleProductsControllerthat is rendered in theindextemplate maybe? Anything stand out for that situation?
I’m probably overthinking this a bit, as it does work fine now. Any advice or comments on this setup would be very much appreciated.
Thanks!