Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 instance.$destroy 方法 #221

Open
fengzilong opened this issue Aug 18, 2018 · 5 comments
Open

增加 instance.$destroy 方法 #221

fengzilong opened this issue Aug 18, 2018 · 5 comments

Comments

@fengzilong
Copy link
Member

fengzilong commented Aug 18, 2018

一般组件实例上的方法都是以 $ 开头的,比如 $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 方法呢

@leeluolee
Copy link
Member

leeluolee commented Aug 20, 2018

bingo !! 之前也想了

主要是前向兼容的问题。

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

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

@leeluolee
Copy link
Member

#221 看这个issue

@fengzilong
Copy link
Member Author

原来之前已经提过了 or2

@leeluolee
Copy link
Member

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

@fengzilong
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants