From 0cb9d4f91a3bc31194f401a071b726b4095ec4b4 Mon Sep 17 00:00:00 2001 From: dntzhang Date: Thu, 14 Mar 2019 17:56:22 +0800 Subject: [PATCH] update main concepts --- docs/main-concepts.cn.md | 193 +++++++++++++++++++++++---------------- docs/main-concepts.md | 43 ++------- 2 files changed, 122 insertions(+), 114 deletions(-) diff --git a/docs/main-concepts.cn.md b/docs/main-concepts.cn.md index 317f82e3e..278726cb7 100644 --- a/docs/main-concepts.cn.md +++ b/docs/main-concepts.cn.md @@ -308,101 +308,133 @@ define('my-element', class extends WeElement { ### Store -Omi 的 Store 体系: 从根组件注入,在所有子组件可以共享。使用起来非常简单: +Store 是 Omi 内置的中心化数据仓库,他解决和提供了下面问题和能力: -```js -import { define, render, WeElement } from 'omi' +* 组件树数据共享 +* 数据变更按需更新依赖的组件 -define('my-hello', class extends WeElement { - render() { - //任意子组件的任意方法都可以使用 this.store 访问注入的 store - return
{this.store.name}
+![](https://github.com/Tencent/omi/raw/master/assets/store.jpg) + +## 一段代码完全上手 Store + +```jsx +import { render, WeElement, define } from '../../src/omi' + +define('my-counter', class extends WeElement { + static use = [ + { count: 'count' } + ] + + add = () => this.store.add() + sub = () => this.store.sub() + + addIfOdd = () => { + if (this.use.count % 2 !== 0) { + this.store.add() + } } -}) -define('my-app', class extends WeElement { - handleClick = () => { - //任意子组件的任意方法都可以使用 this.store 访问注入的 store - this.store.reverse() - this.update() + addAsync = () => { + setTimeout(() => this.store.add(), 1000) } render() { return ( +

+ Clicked: {this.use.count} times + {' '} + + {' '} + + {' '} + + {' '} + +

+ ) + } +}) + +render(, 'body', { + data: { + count: 0 + }, + sub() { + this.data.count-- + }, + add() { + this.data.count++ + }, +}) +``` + +* 通过 `static use` 声明依赖的 path +* `store` 通过 render 的第三个参数从根节点注入到所有组件。 + +下面举一个复杂的 `use` 例子: + +```jsx +static use = [ + 'count', //直接字符串,JSX 里可通过 this.use[0] 访问 + 'arr[0]', //也支持 path,JSX 里可通过 this.use[1] 访问 + //支持 json + { + //alias,JSX 里可通过 this.use.reverseMotto 访问 + reverseMotto: [ + 'motto', //path + target => target.split('').reverse().join('') //computed + ] + }, + { name: 'arr[1]' }, //{ alias: path },JSX 里可通过 this.use.name 访问 + { + //alias,JSX 里可通过 this.use.fullName 访问 + fullName: [ + ['userInfo.firstName', 'userInfo.lastName'], //path array + (firstName, lastName) => firstName + lastName //computed + ] + }, +] +``` + +下面看看 JSX 中使用: + +```jsx +... +... +render() { + return ( +
+ + {this.use[0]} +
- - + {this.use[1]} +
- ) - } -}) - -const store = { - name: 'abc', - reverse: function() { - this.name = this.name.split("").reverse().join("") - } +
{this.use.reverseMotto}
+
{this.use.name}
+
{this.use[3]}
+
+ {this.use.fullName} + +
+
+ ) } -//通过第三个参数注入 -render(, document.body, store) +... +... ``` -与全局变量不同的是, 当有多个根节点的时候就可以注入多个 store,而全局变量只有一个。 +如果不带有 alias ,你也可以直接通过 `this.store.data.xxx` 访问。 - - +总结: 只要注入组件的 path 等于 use 里声明 或者在 use 里声明的其中 path 子节点下就会进行更新! ### Slot diff --git a/docs/main-concepts.md b/docs/main-concepts.md index a11db36d8..93558ea31 100644 --- a/docs/main-concepts.md +++ b/docs/main-concepts.md @@ -325,7 +325,7 @@ The `classNames` is the same as [classnames](https://github.com/JedWatson/classn ### Store -Omi Store Architecture: Injected from the root component and shared across all subcomponents. It's very simple to use: + Unlike global variables, when there are multiple root nodes, multiple stores can be injected. -