Ember如何在Controller中使用模型字段值

时间:2016-07-16 10:42:23

标签: ember.js ember-data emberfire

我是Ember的新手并且已经打了几个小时我怎么能在控制器中使用模型字段的值?

这是我的模特:

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()
});

这是我的路线:

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)
        })
    });
   }
});

这是我的控制者:

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);
        },
  }
});

我希望此性别数组中的值1可以是model.customer.id_gender的值吗?

1 个答案:

答案 0 :(得分:0)

是的,你可以。

像这样更改selection

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

请查看this twiddle