regularjs/regular

增加 instance.$destroy 方法

Opened this issue · 5 comments

一般组件实例上的方法都是以 $ 开头的,比如 $watch、$emit、$update
但是销毁的方法却没有 $ 前缀,感觉用的时候会搞混

init: function(){},
config: function(){},
destroy: function(){
// destroy event wont propgation;
this.$emit("$destroy");
this._watchers = null;
this._watchersForStable = null;
this.group && this.group.destroy(true);
this.group = null;
this.parentNode = null;
this._children = null;
this.$root = null;
this._handles = null;
this.$refs = null;
var parent = this.$parent;
if(parent && parent._children){
var index = parent._children.indexOf(this);
parent._children.splice(index,1);
}
this.$parent = null;
if (this.devtools) {
this.devtools.emit("destroy", this)
}
this._handles = null;
this.$phase = "destroyed";
},

这里的 destroy 和 config/init 应该是同样性质的,都是生命周期钩子,文档应该写在生命周期那个章节,但是同时它又会被当做方法来调用(比如在路由的场景下,组件不需要 cache 的话,就会主动 destroy)

https://regularjs.github.io/reference/

文档中的实例方法也没有提到 destroy,所以是不是可以考虑加个 $destroy 方法呢

bingo !! 之前也想了

主要是前向兼容的问题。

$destory(){
   this.destory();
  // other distroy logic 内建的
}

这样就不需要考虑this.supr()的问题了, 之前碰到很多忘记supr的问题, 差不多是FAQ了

#221 看这个issue

原来之前已经提过了 or2

我的想法是新增生命周期。 这样新版本就使用recycle, 也可以使用 destroy + supr, 不需要任何的upgrade操作。 同时增加更加一致性的$destory @fengzilong 你觉得呢, 实现上都简单,关键哪种更友好些

这样也可以,给 destroy 加个 deprecated warning,尽量用新的 recycle 生命周期~