首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算属性_super

计算属性_super
EN

Stack Overflow用户
提问于 2016-02-07 20:39:40
回答 2查看 733关注 0票数 1

我想知道是否有一种获得计算属性的_super的方法?

例如:

controllers/core-controller.js

代码语言:javascript
复制
export default Controller.extend({
    myAttribute: computed(function() {
        return {
            bar: this.get('bar')
        };
    });
});

controllers/my-controller.js

代码语言:javascript
复制
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' });
    });
});

这样做最好的方法是什么?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-07 21:38:07

您可以通过调用this._super(...arguments)来做到这一点

代码语言:javascript
复制
import CoreController from 'controllers/core-controller';

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

也是在这个圈子里降级的:https://ember-twiddle.com/dba33b9fca4c9635edb0

票数 6
EN

Stack Overflow用户

发布于 2016-02-07 20:50:28

您可以在初始化期间定义该属性:

代码语言:javascript
复制
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' });
    }));
  })
})

警告:在init期间,您只会得到一次superValue。如果myAttribute有依赖项,它可以重新计算,但是它总是从超类获得要合并的原始值,而不是更新的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35258849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档