Is it bad to inject the store into a model?


#1

I have a ShoppingCart model, which hasMany('cartItem'). I want the model to have an addProduct method, that will either update the corresponding cartItem, or create a new one. One approach would be something like the following:

App.ShoppingCart = DS.Model.extend({
  orderItems: DS.hasMany('cartItem'),

  addProduct: function(product) {
    var items = this.get('cartItems');
    var itemForProduct = items.filterBy('product', product).get('firstObject');
    if (Em.isNone(itemForProduct)) {
      itemForProduct = this.store.createRecord('cartItem', { product: product });
      items.pushObject(itemForProduct);
    }
    itemForProduct.incCount();
  }

});

App.CartItem = DS.Model.extend({
  product: DS.belongsTo('product'),
  count: DS.attr('number', { default: 0 }),

  incCount: function(){
    var count = this.get('count');
    this.set('count', count + 1);
  }
})

For this to work I would need to inject the store into my ShoppingCart model:

// something like this??
App.inject('shoppingCart', 'store', 'store:main');

Is this a Bad Thing to do? I realise I could also put the addProduct method on a controller instead, but it seems like it should belong on the model. Is there a ‘more Ember’ way?