docs: update readme

This commit is contained in:
dntzhang 2019-10-29 13:30:31 +08:00
parent cf13ff8eef
commit ade47b75ef
2 changed files with 40 additions and 41 deletions

View File

@ -45,7 +45,10 @@ render(<my-counter />, 'body', new Store)
* `<my-counter></my-counter>` 可以用于任意框架或者无框架,比如 `document.createElement('my-counter')`
你也可以通过 use 去实现计算属性,比如:
你也可以使用 `useSelf`, `useSelf` 只会更新自身,不更新子组件。使用 `useSelf` 的时候在 JSX 里通过 `usingSelf` 访问对应属性。
你也可以通过 `compute` 去实现 `computed` 计算属性,比如:
```jsx
define('my-counter', _ => (
@ -53,23 +56,18 @@ define('my-counter', _ => (
<button onClick={_.store.sub}>-</button>
<span>{_.store.data.count}</span>
<button onClick={_.store.add}>+</button>
<div>Double: {_.using.doubleCount}</div>
<div>Double: {_.computed.doubleCount}</div>
</div>
), {
use: [
'count',
{
doubleCount: [
'count',
count => count * 2
]
}]
use: ['count'],
compute: {
doubleCount() {
return this.count * 2
}
}
})
```
你也可以使用 `useSelf`, `useSelf` 只会更新自身,不更新子组件。使用 `useSelf` 的时候在 JSX 里通过 `usingSelf` 访问对应属性。
路径也是支持的,比如下面的例子:
```js
@ -90,13 +88,12 @@ define('my-counter', _ => (
), {
use: [
'list[0].name', //可以通过 this.using[0] 访问
{
// fullName 是别名, 可以通过 this.using.fullName 访问
fullName: [
['list[0].name.first', 'list[0].name.last'],
(first, last) => first + last
]
}]
],
compute: {
fullName() {
return this.list[0].name.first + this.list[0].name.last
}
}
})
```

View File

@ -45,7 +45,9 @@ render(<my-counter />, 'body', new Store)
* `<my-counter></my-counter>` can be used in any framework or no framework, such as `document.createElement('my-counter')`
You can also implement computed props through use, such as:
You can also use `useSelf`, `useSelf` only updates itself. When using `useSelf`, the corresponding attributes are accessed through. `usingSelf` in JSX.
You can also implement `computed` props through `compute`, such as:
```jsx
define('my-counter', _ => (
@ -53,22 +55,18 @@ define('my-counter', _ => (
<button onClick={_.store.sub}>-</button>
<span>{_.store.data.count}</span>
<button onClick={_.store.add}>+</button>
<div>Double: {_.using.doubleCount}</div>
<div>Double: {_.computed.doubleCount}</div>
</div>
), {
use: [
'count',
{
doubleCount: [
'count',
count => count * 2
]
}]
use: ['count'],
compute: {
doubleCount() {
return this.count * 2
}
}
})
```
You can also use `useSelf`, `useSelf` only updates itself. When using `useSelf`, the corresponding attributes are accessed through. `usingSelf` in JSX.
Path is also supported:
```js
@ -88,14 +86,13 @@ define('my-counter', _ => (
...
), {
use: [
'list[0].name', //Direct string, accessible through this.using[0]
{
//aliasaccessible through this.using.fullName
fullName: [
['list[0].name.first', 'list[0].name.last'],
(first, last) => first + last
]
}]
'list[0].name', //Direct string path dep, accessible through this.using[0]
],
compute: {
fullName() {
return this.list[0].name.first + this.list[0].name.last
}
}
})
```
@ -165,8 +162,13 @@ define('my-component', _ => (
...
...
), {
use: ['path', 'path.a', 'path[1].b'],
use: ['count', 'path.a', 'path[1].b'],
useSelf: ['path.c', 'path[1].d'],
compute: {
doubleCount() {
return this.count * 2
}
},
css: 'h1 { color: red; }',
propTypes: {