简体   繁体   中英

Ember computed property _super

I'd like to know if there is a way to get the _super for a computed property?

For example:

controllers/core-controller.js

export default Controller.extend({
    myAttribute: computed(function() {
        return {
            bar: this.get('bar')
        };
    });
});

controllers/my-controller.js

import CoreController from 'controllers/core-controller';

export default CoreController.extend({
    myAttribute: computed(function() {
        let super = this.get('_super') //I'm trying to get the myAttribute object from the CoreController
        return merge(super, { foo: 'foo' });
    });
});

What would be the best way to do that?

Thanks.

You can do it by calling this._super(...arguments) :

import CoreController from 'controllers/core-controller';

export default CoreController.extend({
    myAttribute: computed(function() {
        let superValue = this._super(...arguments);
        return merge(superValue, { foo: 'foo' });
    });
});

Also demoed in this twiddle: https://ember-twiddle.com/dba33b9fca4c9635edb0

You could define the property during initialization:

export default CoreController.extend({
  defineMyAttribute: Ember.on('init', function() {
    const superValue = this.get('myAttribute');

    Ember.defineProperty(this, 'myProperty', Ember.computed(function() {
      return merge(superValue, { foo: 'foo' });
    }));
  })
})

Caveat: you'd only get superValue once, during init. If myAttribute had dependencies, it could recompute, but it would always get the original value from the super class to merge in, never an updated one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM