0

I have a list of "Candidato" that I want to display in a dropdown box. Here's an example of a Candidato:

{"id": 3, "nombre": "Ivonne Álvarez Garcia", "sexo": 0, "confirmado": true}

Here's another one:

{"id": 1, "nombre": "Margarita Arellanes Cervantes", "sexo": 0, "confirmado": false},

In my template I have this:

{{view "select" content=model.candidatosPAN optionValuePath="content.id" optionLabelPath="content.nombre" selection=newParams.candidatoPAN}}<br/>

The thing is I want the "option label" to be variable, depending on the nombre and confirmado. So, for example, the option for Ivonne will be displayed as "Ivonne Álvarez Garcia (*)" ... because she is "confirmado". On the other hand, the option for Margarita would be: "Margarita Arellanes Cervantes" because she is not "confirmado".

What's the simplest and ember-ish way to do that?

Thanks!

Cokorda Raka
  • 4,375
  • 6
  • 36
  • 54

1 Answers1

1

One solution would be to wrap the content in a list property somewhere in your controller defining the labels, so for example:

// in your controller
candidatos: Ember.computed.map('candidatosPAN', function(candidato) {
    var suffix = candidato.get('confirmado') ? ' (*)' : '';
    return {
        id: candidato.get('id'),
        label: candidato.get('nombre') + suffix
    };
});

// in your template
{{view "select" content=candidatos optionValuePath="content.id" optionLabelPath="content.label"}}

Instead of the selection attribute you can use the value attribute to bind the selection with the id of the candidate. So instead of selecting a candidate you now select a candidate id.

jcbvm
  • 1,640
  • 1
  • 15
  • 22
  • Hi, I also considered that approach. However because of the way the selected option is bound (it is bound directly with the underlying model), I had to use the original candidatosPAN I drew from the route's model. Is there any approach that involves defining a simple extension of Ember.Select maybe? – Cokorda Raka Feb 06 '15 at 20:45
  • If you want to update your model directly, you can add an observer to your controller which observes the selected id. In the observer you have to find the correct candidate and update your model with the candidate. – jcbvm Feb 06 '15 at 20:50