Model relation passed to component not available


#1

Hello!

I have a trouble with accesing relation on model passed to component/ Model example:

name: DS.attr('string'),
rel: belongsTo('something')

component calling:

{{cp m=model}}

component template (here all fine, i see relation visible property value):

{{m.rel.visible}}

component.js (here returnerd value is undefined, why?)

export default Ember.Component.extend({
    isVisible: Ember.computed(function(){
    return this.get('m').get('rel').get('visible');    
  }),
}

P.S. Cannot format code…


#2

You need to let Ember know what properties your computed property depends on, also you don’t need to use get so many times. This should work:

isVisible: Ember.computed(‘m.rel.visible’, function(){ return this.get(‘m.rel.visible’);
})

Better yet, you can just use alias since that’s all you are effectively doing :slight_smile:

isVisible: Ember.computed.alias(‘m.rel.visible’)


#3

All working! Nice solution, can i inject all properties of m.rel or 2-3 only? Maybe something ‘m.rel.$each’ ?


#4

Depends on your use-case, typically you would want to use a normal computed property when you need some additional logic or manipulation. If all you want is to access the value then alias is a better fit.

This is a good read that might explain it a bit more - https://guides.emberjs.com/v2.3.0/object-model/computed-properties-and-aggregate-data/