Skip to content

create model的维度 #13

@ryuever

Description

@ryuever

背景

目前在relinx中store的引入方式是通过useRelinx(storeName)的方式;目前它存在一个问题是

1:parent/children 并没有约束必须应用同一个store;

const A = observe(() => {
  const [state] = useRelinx('a')

  const { shouldDisplay } = state

  if (!shouldDisplay) return null

  return (
    <B />
  )
})

const B = observe(() => {
  const [state] = useRelinx('c')
  return null
})

比如上面的形式,B是可以引入其它的model c;如果不考虑到解偶的话,这个没有问题,但是现在出现了一个场景是模块化以及嵌套模块的堆叠;也就是会存在下面的场景

render_image

它表示的是A组件存在于B,C,D这几个组件;在halation约定的是register.js中声明需要加载的model类如下面的形式

export default function PluginComponent() {
  return {
    name: 'plugin-b',
    getModel: () => require('./model'),
    getComponent: () => require('./index'),
  }
}

这个时候你会发现,其实A模块的model如果是共享同一个model instance已经不在合适;所以,接下来针对每一个A都要创建不同的model instance;这个需要配合halation的配置部分

const halationState = [{
  name: 'B',
  key: 'b-1',
  prevSibling: null,
  nextSibling: 'c-1',
  children: ['a-1'],
  type: 'block',
}, {
  name: 'A',
  key: 'a-1',
  prevSibling: null,
  nextSibling: null,
  children: [],
  type: 'block',
}, {
  name: 'C',
  key: 'c-1',
  prevSibling: 'b-1',
  nextSibling: 'd-1',
  children: ['a-2'],
  type: 'block',
}, {
  name: 'A',
  key: 'a-2',
  prevSibling: null,
  nextSibling: null,
  children: [],
  type: 'block',
}, {
  name: 'D',
  key: 'd-1',
  prevSibling: 'c-1',
  nextSibling: null,
  children: ['a-3'],
  type: 'block',
}, {
  name: 'A',
  key: 'a-3',
  prevSibling: null,
  nextSibling: null,
  children: [],
  type: 'block',
}]

写法变更

1: 模块在进行store的引入的时候不再需要提供storeName

const A = observe(() => {
  const [state] = useRelinx()
  return null
})

2: 如果需要进行dispatch的话,dispatch的type namespace和它在halataionState中定义的key是一致的

dispatch({
  type: 'a-1/setProps', 
  payload: { value: 2 }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions