Get specific object form 'needs' controller

Hi,

I’m new with Ember, so I guess there’s a simple answer to that:

I’m using ‘needs’ declaration, to reach from a template to the data of another controller (let’s call it ‘controllerB’).

The issue is that the data I’m consuming from this ‘controllerB’, is not an object , but an array. So in the template I can reach the whole array by {{controllers.controllerB}}, or loop over all the objects in the array using {{#each item in controllers.controllerB}}.

But - how can I reach a specific object in that array (by ID)??

Thanks!

Handlebars cant do any kind of variable based logic other than boolean block Ifs (by design), so whatever view or controller is sat between your handlebars template and ControllerB should implement some kind of getsingleobject method, perhaps using an observed property to decide which item to return ala:

ControllerB:
  listOfItems: []

ControllerC
  needs: ['ControllerB],
  selectedId: null,
  selectedItem : function() {
    if(this.get('selectedId') {
      return this.get('controllers.controllerB.listOfItems)[this.get('selectedId')];
    }
    
  }.property('selectedId')

 itemView
  click: function() {
    //Assumes controller is ControllerC
    this.get('controller').set('selectedId', model.id);
  }

then within the context of controllerC {{controller.selectedItem}}

This is messy and there should be more finesse in a real world solution but hopefully it gets the point across:

NO (non-boolean) LOGIC IN TEMPLATES

1 Like

Great, thanks. That’s exactly what I needed.