1

是否可以有条件地扩展 Ember 类?像这样的东西:

A.reopen({
  if (condition) {
    init: function() {
      this.super();
      // some functionality
    }.on('didInsertElement');
  }
})

目前我有这样的模式:

A.reopen({
  init: function() {
    this.super();
    if (condition) {
      // some stuff
    }
  }.on('didInsertElement'),

  cleanup: function() {
    if (condition) {
      // some stuff
    }
  }.on('willDestroyElement')
})

我猜想如果我可以扩展 A 类,我可以像这样简化我的模式:

A.reopen({
  if (condition) {
    init: function() {
      this.super();
      // some functionality
    }.on('didInsertElement'),

    clear_up: function() {
      // some stuff
    }.on('willDestroyElement')
  }
})

在插件中为话语制作的所有类扩展

4

1 回答 1

0

看起来你想要在 Java 中被称为抽象类的东西。

Ember.Component.extend({ // abstract class

  doSomeInit: Ember.K,

  doSomeCleaning: Ember.K,

  didInsertElement: function() {
    this.super(..arguments);
    this.doSomeInit();
  },

  willDestroyElement: function() {
    this.doSomeCleaning();
  }
})

// class A
Ember.Component.extend(MyIncompleteClass, {

  doSomeInit: function() { /* etc */ },

  doSomeCleaning: function() { /* etc */ }

});

// class B
Ember.Component.extend(MyIncompleteClass, {

  doSomeInit: function() { /* etc */ },

  doSomeCleaning: function() { /* etc */ }

});

旁注:最好覆盖生命周期钩子而不是使用 Ember.on,以保证执行顺序;在同一事件有多个 Ember.on 的情况下。

于 2016-10-23T20:35:34.980 回答