DVG
DVG

Reputation: 17480

Ember Mixins and the needs property

I have sort of a general question on how the controller needs property works with regard to mixins

Lets say I have a shopping card model that resides in the application controller so it's available everywhere in the app

// application route
export default Ember.Route.extend({
  setupController: function(controller, model) {
    this._super(controller, model);
    controller.set('cart', this.store.createRecord('cart'));
  }
});

Now any other controller that needs to use the cart, I want to provide a mixin:

// mixins/cart-access
export default Ember.Mixin.create({
  needs: ['application'];
  cart: Ember.computed.alias('controllers.application.cart')
});

// some controller
export default Ember.Controller.extend(CartAccess, {});

This is all well and good, but will it cause problems if in another controller I set the needs property to something else?

// some other controller
export default Ember.Controller.extend(CartAccess, {
  needs: ['some-other-controller'] // not inlcuding application
});

Upvotes: 1

Views: 470

Answers (1)

DVG
DVG

Reputation: 17480

Went ahead and did an experiment, and the needs from the mixin will be merged with the needs from the controller.

Example: https://github.com/DVG/need-experiment

//application route
export default Ember.Route.extend({
  setupController: function(controller, model) {
    this._super(controller, model);
    controller.set('hello', "Hi!")
  }
});

//hi mixin
export default Ember.Mixin.create({
  needs: ['application'],
  hello: Ember.computed.alias("controllers.application.hello")
});

//people contntroller
import Hi from 'needs/mixins/hi';
export default Ember.Controller.extend(Hi,{});

//posts controller
import Hi from 'needs/mixins/hi';
export default Ember.Controller.extend(Hi, {
  needs: ['something-else']
});

//posts.hbs
{{hello}}
{{needs}}

The posts template displays "Hi!" and application,something-else

Upvotes: 3

Related Questions