-1

I am a newbie on Ember and break my head already a couple of hours how i can use the value of a model field in a controller ?

This is my model :

import DS from 'ember-data';

export default DS.Model.extend({
    id_customer: DS.attr('number'),
    id_default_group: DS.attr('number'),
    id_lang: DS.attr('number'),
    id_gender: DS.attr('number'),
    active: DS.attr('boolean'),
    email: DS.attr(),
    firstname: DS.attr(),
    lastname: DS.attr(),
    company: DS.attr(),
    birthday: DS.attr('date'),
    date_add: DS.attr('date'),
    date_upd: DS.attr('date'),
    max_payment_days: DS.attr('number'),
    newsletter: DS.attr('boolean'),
    note: DS.attr(),
    website: DS.attr()
});

This is my route :

import Ember from 'ember';
export default Ember.Route.extend({
    beforeModel: function(){

        if(!this.get('session.isAuthenticated')){
            this.transitionTo('application');
        }
   }, 

 model(params) {
    return Ember.RSVP.hash({
        customer: this.store.findRecord('customer', params.id),
        address: this.store.query('address', {
            orderBy: 'id_customer_fb',
            equalTo: parseInt(params.id)
        })
    });
   }
});

This is my controller :

import Ember from 'ember';


 const genders = [
    { title: 'Dhr.',  id_gender: '1' },
    { title: 'Mevr.',  id_gender: '2' },
 ];



 export default Ember.Controller.extend({

     genders: genders,
     selection: genders[1]**,   <== THIS '1' MUST BE REPLACED WITH THE VALUE OF models.customer.id_gender ?????**


    actions: {
        chooseDestination(genders) {
        this.set('selection', genders);
        },
  }
});

I would that the value 1 in this genders array could be the value of model.customer.id_gender ?

Rudi Werner
  • 99
  • 1
  • 9

1 Answers1

0

Yes you can.

Change selection like this :

selection: Ember.computed(function(){
    let ret = this.get('genders').filterBy('id_gender', this.get('model.customer.id_gender'));    
    return ret.objectAt(0);
  })

Please take a look at this twiddle

Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
  • Hello, thanks to your answer i found the solution (also needed to change id_gender from stirng to number in tre array ! BUT ... i use this to fil a select with a value that complains a record in a model ! so far so good but it only works the first time i go from the table to the form ? then it stays always the same value... has this something to do with the singleton aspect from Ember and ... how an i fix this ?? – Rudi Werner Jul 16 '16 at 13:30
  • @RudiWerner Please show your template or better provide in twiddle – Ebrahim Pasbani Jul 16 '16 at 15:33