1

因此,在与默认控制器不同的控制器中使用计算属性时会出现问题。

我有这个模型:

export default DS.Model.extend({
    firstName:       DS.attr('string'),
    lastName:        DS.attr('string'),
    email:           DS.attr('string'),
    emailOptIn:      DS.attr('boolean'),
    emailVerified:   DS.attr('boolean'),
    receivedWelcome: DS.attr('boolean'),

    locations:            DS.hasMany('location'),
    serviceRelationships: DS.hasMany('service-relationship'),
    phoneNumbers:         DS.hasMany('phone-number'),

    fullName: function(){
        return this.get('firstName') + ' ' + this.get('lastName');
    }.property('firstName', 'lastName')
});

哪个在contact.js(我们使用的是 ember-cli)。

然后我有这个控制器:

var NavBarController = Ember.Controller.extend({
    fullName: '',
    init: function(){
        var contactId = this.session.contactId;
        var self = this;
        this.store.find('contact', contactId).then(function(contact){
            self.set('fullName', contact.get('fullName'));
        });
})

export default NavBarController;

它不会在模板中呈现任何内容。但是,如果我将其更改为:

    fullName: '',
    init: function(){
        var contactId = this.session.contactId;
        var self = this;
        this.store.find('contact', contactId).then(function(contact){
            window.setTimeout(function(){
                self.set('fullName', contact.get('fullName'));
            }, 100);
        });
})

然后它渲染{{fullName}}车把就好了。

问题:为什么then没有timeout?

4

0 回答 0