regularjs/regular

增加一个新的LifeCycle方法【modifyBodyComponent】,支持修改其包裹的组件

Closed this issue · 1 comments

正在实现一个名为modifyBodyComponent(初定)的新LifeCycle相关方法支持(如init, config)。 这个方法不常用,但是非常有用,属于能力扩展。

例如一个Modifier组件,实现了类似modifyBodyComponent这个方法后,将可以使其所有包裹的组件(包括组件内部的子组件)都拥有dispatch的方法,这个dispatch与modifier组件本身的store属性直接相关

  var Modifier = Regular.extend({
      name: 'modifier'
      template: '{#include this.$body}',
      config: function(){
        this.store = createStore();
      },
      modifyBodyComponent: function( component ){
        component.dispatch =  this.store.dispatch.bind(store)
      }
    });

模板中

<modifier>
   <other-component/>
    <other-component2/>
</modifier>

这个生命周期支持可以带来多种可能性,如

  • 全局事件广播
  • redux等全局数据解决方案的store绑定
  • 组件实例函数动态扩展(这个Mixin当然也可以做,但是这个可以将绑定延迟到初始化时,而非组件定义阶段)
  • 跨组件局部数据传递, 避免大量重复数据的层层传递

本质上其实是: 通过这种方式,我们可以将耦合延迟到初始化时,而非在定义时候。

发布时间
将会在0.6版本提供。

done